472,960 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 software developers and data experts.

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 1751

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

Similar topics

15
by: DanielEKFA | last post by:
Hey there :) Just joined the group, looking for a resolution to a problem I and my three groups members are having (we're students, making an FTP-like server/client as an exam project). We're...
0
by: john.leonard.ireland | last post by:
Hi all, I don't know what I'm missing here, but if anyone could help me out I'd really appreciate it. Basically, everything is fine, compiles, installs, server runs, but the interactive mysql...
1
by: sysxperts | last post by:
Hello, Having an issue that is specific to PHP compiled with PGSQL support with versions noted in subject line. I understand that there are many variables to consider here but believe I have...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
6
by: g35rider | last post by:
Hi, sometimes due to unchecked code I get segmentation faults once in a while and would like to be able to catch them and do some cleanup on things. This segmentation fault could be anywhere in the...
8
by: lawrence k | last post by:
We made some changes to our server yesterday, and ever since, every single installation of WordPress that was on the server has stopped running. Other PHP scripts still run fine, but WordPress is...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
4
by: Michael B. Trausch | last post by:
Is there a way to debug scripts that cause segmentation faults? I can do a backtrace in gdb on Python, but that doesn't really help me all that much since, well, it has nothing to do with my...
8
by: Mathias | last post by:
Dear ng, I use the thread module (not threading) for a client/server app where I distribute large amounts of pickled data over ssh tunnels. Now I get regular Segmentation Faults during high load...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.