Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get error "Segmentati on Fault"
This is Code for server:
/* server_it.c: DST iterative echo server
to be linked with DST_sock */
#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>
#include <sys/types.h>
#include <fcntl.h>
#define EOB "Client:i$$ i"
#define UDP_PORT 5555
#define KB_size 100
#define MAX_CLIENT 10
int ClientNo = 0;
////////////////////////////////////////////
int openService (int NoClients)
{int sock;
int intPort;
char host[80];
char KB_buff[KB_size];
sock = sockTCP_create( );
printf("Please 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 open_UDP()
{
int sockfd;
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr , sizeof(servaddr ));
servaddr.sin_fa mily = AF_INET;
servaddr.sin_ad dr.s_addr = htonl(INADDR_AN Y);
servaddr.sin_po rt = htons(UDP_PORT) ;
sock_bind(sockf d, &servaddr, sizeof(servaddr ));
return sockfd;
}
////////////////////////////////////////////
int getClient(MSsoc k)
{char *cli_name;
struct sockaddr_in *client_EP;
int sock;
char *msg =
"Please 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;
}
This is code for Client:
/* Unix Client program client_TCP.c */
/* To be linked with DST_sock */
#include <sys/types.h> // for type
#include <sys/socket.h> // for socket API
#include <netinet/in.h> // for address
#include <arpa/inet.h> // for sockaddr_in
#include <stdio.h> // for printf()
#include <stdlib.h> // for atoi()
#include <string.h> // for strlen()
#include <unistd.h> // for close()
#define EOB "Client:i$$ i"
#define KB_size 80
#define UDP_PORT 5555
char KB_buff[KB_size];
int open_UDP()
{
int sockfd;
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr , sizeof(servaddr ));
servaddr.sin_fa mily = AF_INET;
servaddr.sin_ad dr.s_addr = htonl(INADDR_AN Y);
servaddr.sin_po rt = htons(UDP_PORT) ;
sock_bind(sockf d, &servaddr, sizeof(servaddr ));
return sockfd;
}
////////////////////////////////////////////
int open_connect(st ruct sockaddr_in *server_EP)
{
int sock;
int intPort;
struct hostent *phe;
int nc;
char KB_buff[80];
printf ("Please enter port number: ");
fgets(KB_buff,1 0,stdin);
intPort = atoi (KB_buff);
printf ("Please enter host name : ");
nc = 0;
while ((KB_buff [nc] = getc(stdin))
!= '\n') nc += 1;
KB_buff [nc] = '\0';
setHostByName (server_EP, KB_buff,
intPort);
sock = sockTCP_create( );
sock_connect(so ck, server_EP);
return (sock);
}
////////////////////////////////////////////
void getEcho (int sock)
{
struct sockaddr_in sender_EP;
char *msg;
msg = (char *)readMessage (sock);
if (msg != NULL) {
printf ("Please type message");
printf ("return\n") ;
}
fgets (KB_buff, KB_size, stdin);
while (strlen(KB_buff ) > 1) {
sendtoDST (sender_EP,sock , KB_buff);
msg = (char *)recvfromDST (
&sender_EP, sock);
if (msg != NULL) printf (
"%s==>%s\n" , inet_ntoa(
sender_EP.sin_a ddr),msg);
fgets (KB_buff, KB_size, stdin);
}
}
////////////////////////////////////////////
int main()
{
int MC_socket,MC_UD P;
struct sockaddr_in server_EP;
//tcp
MC_socket = open_connect(&s erver_EP);
MC_UDP = open_UDP();
//udp
while(1)
{
printf("please input message:\n");
fgets (KB_buff, KB_size, stdin);
writeMessage(MC _socket,KB_buff ,KB_size);
//recvfrom udp
recvfromDST(ser ver_EP,MC_UDP);
}
}
This is error message when i get when i run both programs.
When i want to connect to the server program from the client program. I get error "Segmentati on fault"
This is when i want to connect from the client program the staff that i run from the client program:
Enter port number: 5555
Enter host name : 11a46-17
This is what happens whens in server program:
Enter port: 5555
ITserver at 11A46-17 [ 5555]
Segmentation fault