473,796 Members | 2,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation faults with client and server

5 New Member
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
Sep 8 '07 #1
0 1814

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

Similar topics

15
2452
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 getting segmentation faults when threading with the following code (this code is not the actual code as it's going to look, but a shortened version for debugging purposes, yet it segfaults the same way as the "real" code):
0
1568
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 client breaks. I have an IA64 Red Hat Linux Enterprise Edition system and I tried to install the binaries recommended on MySQL. However, when I run the mysql client, I get simply:
1
2486
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 narrowed down the Apache Client Cert failures to my PHP/PGSQL build. 1. Apache PHP without PGSQL works as expected using client certificates 2. Apache PHP with PGSQL works fine with standard SSL connections 3 Apache PHP with PGSQL fails with...
18
26124
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)? Thanks.
6
5196
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 code. Could it be caught like a signal and perform cleanup then? Or would I have to put every piece of function code in try and catch blocks? Thanks Ankur
8
4933
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 dead. I logged into the server using ssh and looked the Apache error_log. The only thing there was a whole bunch of lines like this: child pid 27827 exit signal Segmentation fault (11)
7
5881
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
4
20920
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 script... :-P Any ideas? Thanks, Mike
8
3645
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 episodes. I use a semaphore to have pickle/unpickle run nonthreaded, but I still get frequent nondeterministic segmentation faults. Since there is no traceback after a sf, I have no clue what exactly happened, and debugging a multithreaded app...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.