Hello everybody
im creating a srever program for my work
that can communicate with the client using udp protocol.
i get couple errors and i do not know hat those error mean and what is the problem with by code.
Need Help
The code:
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <time.h>
#include <netdb.h>
#include <sys/signal.h>
#include <string.h> //for strln()
#include <sys/types.h>
#include <fcntl.h>
#define EOB "Client:$$"
#define KB_size 80
#define MAX_CLIENT 100
int ClientNo = 0;
////////////////////////////////////////////
int openService (int NoClients)
{int sock;
int intPort;
char host[80];
char KB_buff[KB_size];
sock = sockTCP_create( );
printf("Enter port: ");
fgets(KB_buff, KB_size, stdin);
intPort = atoi(KB_buff);
sock_bind(sock, intPort);
sock_listen(soc k, NoClients);
gethostname(hos t,80);
printf("ITserve r at %s [ %d]\n",
host,intPort);
return (sock);
}
////////////////////////////////////////////
int getClient(MSsoc k)
{char *cli_name;
struct sockaddr_in *client_EP;
int sock;
char *msg =
"Type any string or EOB to quit\n";
time_t con_time;
sock = sock_accept(MSs ock, &client_EP);
if (sock == 0) IO_error (0, "accept");
else {
time(&con_time) ;
writeMessage(so ck, msg);
ClientNo += 1;
cli_name = (char *)getNameByEP (*client_EP);
printf ("Client %d (%s-%d)\n", ClientNo,
cli_name, client_EP->sin_port);
printf (" -->connect at %s\n",
asctime (localtime (&con_time))) ;
}
return (sock);
}
////////////////////////////////////////////
int DST_SingEcho (int sock,char *msg)
// Get a message at sock, if message <> EOB
// echo back, else B or "" then quit;
{
int nc;
int quit = 0;
nc = readMessage (sock,msg);
quit = (strncmp(msg,EO B, 9) == 0);
if(quit==0)
return 0;
return nc;
}
////////////////////////////////////////////
int main()
{
int MS_socket,MS_UD P,
CS_socket[MAX_CLIENT],CS_udp[MAX_CLIENT],i,iRet,iMaxSoc k=0;
struct sockaddr_in servaddr, cliaddr[MAX_CLIENT];
time_t cli_time;
fd_set fdRead;
struct timeval tv = {1,1};
char sRecvMsg[1024];
memset((char*)C S_socket,0,size of(int)*MAX_CLI ENT);
memset((char*)C S_udp,0,sizeof( int)*MAX_CLIENT );
MS_socket = openService (0);
MS_UDP = open_UDP();
while (1)
{
FD_ZERO(&fdRead );
FD_SET(MS_socke t,&fdRead);
iMaxSock = MS_socket;
for(i=0;i<MAX_C LIENT;i++)
{
if(CS_socket[i] >0)
{
fcntl(CS_socket[i], F_SETFL, O_NONBLOCK);
iMaxSock = (iMaxSock > CS_socket[i])?iMaxSock:CS_s ocket[i];
FD_SET(CS_socke t[i],&fdRead);
}
}
tv.tv_sec =1;
tv.tv_usec = 0;
iRet = select( iMaxSock +1,&fdRead,NULL ,NULL,&tv);
if(iRet==0)
continue;
else if(iRet >0)
{
if(FD_ISSET(MS_ socket,&fdRead) )
{
CS_socket[i] = getClient(MS_so cket,cliaddr[i]);
}
for(i=0;i<MAX_C LIENT;i++)
{
if((CS_socket[i] > 0) && (FD_ISSET(CS_so cket[i],&fdRead)))
{
//recv
memset(sRecvMsg ,0,sizeof(sRecv Msg));
if(DST_SingEcho (CS_socket[i],sRecvMsg) ==0)
{
close(CS_socket[i]);
CS_socket[i]=0;
}
//multicast
for(i=0;i<MAX_C LIENT;i++)
{
if(CS_socket[i] > 0)
{
sendtoDST (cliaddr[i],CS_udp,sRecvMs g);
}
}
}
}
}
}
return 1;
}
the error code that im getting is:
gcc -o server_it server_it.c DST_sock.o
/tmp/cckBqZJJ.o(.tex t+0x281): In function `main':
server_it.c: undefined reference to `open_UDP'
collect2: ld returned 1 exit status
im creating a srever program for my work
that can communicate with the client using udp protocol.
i get couple errors and i do not know hat those error mean and what is the problem with by code.
Need Help
The code:
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <time.h>
#include <netdb.h>
#include <sys/signal.h>
#include <string.h> //for strln()
#include <sys/types.h>
#include <fcntl.h>
#define EOB "Client:$$"
#define KB_size 80
#define MAX_CLIENT 100
int ClientNo = 0;
////////////////////////////////////////////
int openService (int NoClients)
{int sock;
int intPort;
char host[80];
char KB_buff[KB_size];
sock = sockTCP_create( );
printf("Enter port: ");
fgets(KB_buff, KB_size, stdin);
intPort = atoi(KB_buff);
sock_bind(sock, intPort);
sock_listen(soc k, NoClients);
gethostname(hos t,80);
printf("ITserve r at %s [ %d]\n",
host,intPort);
return (sock);
}
////////////////////////////////////////////
int getClient(MSsoc k)
{char *cli_name;
struct sockaddr_in *client_EP;
int sock;
char *msg =
"Type any string or EOB to quit\n";
time_t con_time;
sock = sock_accept(MSs ock, &client_EP);
if (sock == 0) IO_error (0, "accept");
else {
time(&con_time) ;
writeMessage(so ck, msg);
ClientNo += 1;
cli_name = (char *)getNameByEP (*client_EP);
printf ("Client %d (%s-%d)\n", ClientNo,
cli_name, client_EP->sin_port);
printf (" -->connect at %s\n",
asctime (localtime (&con_time))) ;
}
return (sock);
}
////////////////////////////////////////////
int DST_SingEcho (int sock,char *msg)
// Get a message at sock, if message <> EOB
// echo back, else B or "" then quit;
{
int nc;
int quit = 0;
nc = readMessage (sock,msg);
quit = (strncmp(msg,EO B, 9) == 0);
if(quit==0)
return 0;
return nc;
}
////////////////////////////////////////////
int main()
{
int MS_socket,MS_UD P,
CS_socket[MAX_CLIENT],CS_udp[MAX_CLIENT],i,iRet,iMaxSoc k=0;
struct sockaddr_in servaddr, cliaddr[MAX_CLIENT];
time_t cli_time;
fd_set fdRead;
struct timeval tv = {1,1};
char sRecvMsg[1024];
memset((char*)C S_socket,0,size of(int)*MAX_CLI ENT);
memset((char*)C S_udp,0,sizeof( int)*MAX_CLIENT );
MS_socket = openService (0);
MS_UDP = open_UDP();
while (1)
{
FD_ZERO(&fdRead );
FD_SET(MS_socke t,&fdRead);
iMaxSock = MS_socket;
for(i=0;i<MAX_C LIENT;i++)
{
if(CS_socket[i] >0)
{
fcntl(CS_socket[i], F_SETFL, O_NONBLOCK);
iMaxSock = (iMaxSock > CS_socket[i])?iMaxSock:CS_s ocket[i];
FD_SET(CS_socke t[i],&fdRead);
}
}
tv.tv_sec =1;
tv.tv_usec = 0;
iRet = select( iMaxSock +1,&fdRead,NULL ,NULL,&tv);
if(iRet==0)
continue;
else if(iRet >0)
{
if(FD_ISSET(MS_ socket,&fdRead) )
{
CS_socket[i] = getClient(MS_so cket,cliaddr[i]);
}
for(i=0;i<MAX_C LIENT;i++)
{
if((CS_socket[i] > 0) && (FD_ISSET(CS_so cket[i],&fdRead)))
{
//recv
memset(sRecvMsg ,0,sizeof(sRecv Msg));
if(DST_SingEcho (CS_socket[i],sRecvMsg) ==0)
{
close(CS_socket[i]);
CS_socket[i]=0;
}
//multicast
for(i=0;i<MAX_C LIENT;i++)
{
if(CS_socket[i] > 0)
{
sendtoDST (cliaddr[i],CS_udp,sRecvMs g);
}
}
}
}
}
}
return 1;
}
the error code that im getting is:
gcc -o server_it server_it.c DST_sock.o
/tmp/cckBqZJJ.o(.tex t+0x281): In function `main':
server_it.c: undefined reference to `open_UDP'
collect2: ld returned 1 exit status
Comment