473,473 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error in the Client Program

1 New Member
I wrote this c program to send a struct from the client to a server which has the same struct initialized in it. I used gcc feature of cygwin to compile it and I got the following error. Can anyone tell me wha could possibly have gone wrong because I was not able to figure out anything.

Expand|Select|Wrap|Line Numbers
  1. 348 [main] c 7160 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)      
  2. Segmentation fault (core dumped)  
  3.  
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>    /* standard C i/o facilities */
  2. #include <stdlib.h>    /* needed for atoi() */
  3. #include <unistd.h>    /* Unix System Calls */
  4. #include <sys/types.h>    /* system data type definitions */
  5. #include <sys/socket.h> /* socket specific definitions */
  6. #include <netinet/in.h> /* INET constants and stuff */
  7. #include <arpa/inet.h>    /* IP address conversion stuff */
  8. #include <netdb.h>    /* Name lookups (gethostbyname) */
  9. #include <fcntl.h>      /* Read and Write Fucntions */
  10. #include <string.h>     /* FoR Memset() */
  11.  
  12.  
  13. void DieWithError(char *errorMessage)
  14. {
  15.     perror(errorMessage);
  16.     exit(1);
  17. }
  18.  
  19.  
  20. struct message
  21. {
  22.        int packet_num;
  23.        int syn;
  24.        int ack;
  25.        char * msg;
  26. };
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30.     int sock;
  31.     int fd;
  32.     int x;
  33.     int packet_count;
  34.     struct sockaddr_in echoServAddr;
  35.     struct sockaddr_in fromAddr;
  36.     unsigned short echoServPort;
  37.     unsigned int fromSize;
  38.     char *servIP;
  39.     char *buff;
  40.     struct message recvMsg;
  41.     struct message sendMsg;
  42.     struct message sendMsgBuff;
  43.     int stringLen;
  44.  
  45.     if((argc < 3 ) || ( argc > 4 ))
  46.     {
  47.              fprintf(stderr,"Usage: %s <Server IP> <File name> [<Port Number>]\n",argv[0]);
  48.              exit(1);
  49.              }
  50.     servIP = argv[1];
  51.  
  52.     if(argc == 4)
  53.                       echoServPort = atoi(argv[3]);
  54.     else
  55.                       echoServPort = 7;
  56.  
  57.  
  58.     /* Create a datagram/UDP socket */
  59.     if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
  60.         DieWithError("socket() failed");
  61.  
  62.     memset(&echoServAddr, 0, sizeof(echoServAddr));
  63.     memset(&recvMsg,0,sizeof(recvMsg));
  64.     memset(&sendMsg,0,sizeof(sendMsg));
  65.     echoServAddr.sin_family = AF_INET;
  66.     echoServAddr.sin_addr.s_addr = inet_addr(servIP);
  67.     echoServAddr.sin_port = htons(echoServPort);
  68.  
  69.     fd = open(argv[2],O_RDONLY);
  70.     if(fd==-1)
  71.         DieWithError("File opening failed");
  72.  
  73.     buff = (char*)(malloc(10000));    
  74.     x = read(fd,buff,sizeof(buff));
  75.     if(x==-1)
  76.         DieWithError("File read failed");
  77.  
  78.     packet_count=0;
  79.  
  80.     sendMsg.packet_num = htonl(packet_count);
  81.     sendMsg.syn = 0;
  82.     sendMsg.ack = 1;
  83.     buff = argv[2];
  84.     buff[strlen(buff) + 1] = '\0';
  85.     sendMsg.msg = buff;
  86.  
  87.     /* Send the string to the server */
  88.     if((sendto(sock, (struct  message *) &sendMsg, sizeof(sendMsg), 0, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))) != sizeof(sendMsg))
  89.                      DieWithError("sendto() sent a different number of bytes than expected");
  90.  
  91.     /* Recv a response */
  92.     fromSize = sizeof(fromAddr);
  93.     if(stringLen = recvfrom(sock, (struct  message *) &recvMsg, sizeof(recvMsg), 0, (struct sockaddr*) &fromAddr, &fromSize) != sizeof(sendMsg))
  94.                       DieWithError("recvfrom() failed");
  95.  
  96.     if(recvMsg.ack != sendMsg.ack)
  97.                       DieWithError("Message Not Acknowledged properly");
  98.  
  99.     if (echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
  100.     {
  101.         fprintf(stderr,"Error: received a packet from unknown source.\n");
  102.         exit(1);
  103.     }
  104.  
  105.     /* null-terminate the received data */
  106.     printf("Received Acknowledgement : %s\n", recvMsg.ack);
  107.  
  108.     close(sock);
  109.     exit(0);
  110. }
  111.  
Oct 17 '08 #1
1 1458
donbock
2,426 Recognized Expert Top Contributor
Can you use a debugger to find out whereabouts in your program the error occurs? If not, sprinkle printf's throughout your program and look to see the last printf message before the error message.
Oct 17 '08 #2

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

Similar topics

1
by: Laszlo | last post by:
Hi all, As a novice I installed PostgreSQL 7.2.1 on Win32 and works, Borland C++Builder Enterprise Suite 5.0 (build 12.34) what works too. I decided to combine these two programs and develop a...
8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
3
by: Agnes | last post by:
My client hold its MS SQL server in hkbranch, Both china and hong kong office can run the vb.net application very well via VPN. Now, they want to reduce cost and move the MS SQL server to china 's...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
1
by: Toby | last post by:
Hi, I've managed to get my hands on the ms 2003 toolkit, and have successfully (i think) created a .pyd file in win xp (setup.py is provided intersystems cache): ...
4
by: kalaisuresh | last post by:
hai, in my program i used the DBI module my program running well in terminal(redhat linux 9.0) if i use the same program in interface (use CGI) that time it shows the log error BEGIN...
2
by: pranas | last post by:
I'm not a proffessional programmer and I'm asking for your help. I'm using PHP and running Apache server and Mysql database server on my home network computer to serve my web site. My PHP error log...
4
by: fatboySudsy | last post by:
Hi, I have constructed a client program that has given me some error codes that i just cannot see. I was wondering if a different set of eyes with much more experience than me could help me out. ...
6
by: alho | last post by:
The web service is called by a program running on pocket pc. When to call the web service, the first call is still ok, but for the second or later calls, it will throw "403 Forbidden" WebException....
19
by: Kapps | last post by:
Hi, I'm having a problem with my program not being able to run on certain computers. They have the basics, such as the .Net Frameworks. Upon trying to run the program, it gives the error: ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.