468,268 Members | 1,586 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

Segmentation faults with client and server

5
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 "Segmentation 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(sock, NoClients);
gethostname(host,80);
printf("ITserver 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_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(UDP_PORT);
sock_bind(sockfd, &servaddr, sizeof(servaddr));
return sockfd;
}
////////////////////////////////////////////
int getClient(MSsock)
{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(MSsock, &client_EP);
if (sock == 0) IO_error (0, "accept");
else {
time(&con_time);
writeMessage(sock, 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,EOB, 9) == 0);
if(quit==0)
return 0;
return nc;
}

////////////////////////////////////////////
int main()
{
int MS_socket,MS_UDP,
CS_socket[MAX_CLIENT],CS_udp[MAX_CLIENT],i,iRet,iMaxSock=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*)CS_socket,0,sizeof(int)*MAX_CLIENT);
memset((char*)CS_udp,0,sizeof(int)*MAX_CLIENT);
MS_socket = openService (0);
MS_UDP = open_UDP();
while (1)
{
FD_ZERO(&fdRead);

FD_SET(MS_socket,&fdRead);
iMaxSock = MS_socket;

for(i=0;i<MAX_CLIENT;i++)
{
if(CS_socket[i] >0)
{
fcntl(CS_socket[i], F_SETFL, O_NONBLOCK);
iMaxSock = (iMaxSock > CS_socket[i])?iMaxSock:CS_socket[i];
FD_SET(CS_socket[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_socket,cliaddr[i]);
}

for(i=0;i<MAX_CLIENT;i++)
{
if((CS_socket[i] > 0) && (FD_ISSET(CS_socket[i],&fdRead)))
{
//recv
memset(sRecvMsg,0,sizeof(sRecvMsg));
if(DST_SingEcho(CS_socket[i],sRecvMsg) ==0)
{
close(CS_socket[i]);
CS_socket[i]=0;
}
//multicast
for(i=0;i<MAX_CLIENT;i++)
{
if(CS_socket[i] > 0)
{
sendtoDST (cliaddr[i],CS_udp,sRecvMsg);
}
}
}
}
}

}
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_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(UDP_PORT);
sock_bind(sockfd, &servaddr, sizeof(servaddr));
return sockfd;
}
////////////////////////////////////////////
int open_connect(struct 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,10,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(sock, 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_addr),msg);
fgets (KB_buff, KB_size, stdin);
}
}
////////////////////////////////////////////
int main()
{
int MC_socket,MC_UDP;
struct sockaddr_in server_EP;
//tcp
MC_socket = open_connect(&server_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(server_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 "Segmentation 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
Sep 8 '07 #1
0 1490

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

15 posts views Thread by DanielEKFA | last post: by
18 posts views Thread by Digital Puer | last post: by
7 posts views Thread by pycraze | last post: by
4 posts views Thread by Michael B. Trausch | last post: by
8 posts views Thread by Mathias | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.