Results 1 to 3 of 3
Hi everyone,
I am trying to implement a networking program wherein the same program receives messages and same program replies to the messages to the ip address specified. I'm binding ...
- 01-11-2010 #1Just Joined!
- Join Date
- Jan 2010
- Posts
- 5
datagram socket programming problem
Hi everyone,
I am trying to implement a networking program wherein the same program receives messages and same program replies to the messages to the ip address specified. I'm binding the receiving socket to diff. port and sending socket to diff. port, and using thread to handle the receiving socket.
I am getting "Segmentation fault" at the runtime.
I am a newbie and having hard time figuring out the problem. Will be thankful if anyone can help!
following is the program:
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<pthread.h>
void *Recv_Handle(void *);
int main()
{
int Size;
int rc;
pthread_t thread;
int n, len;
char msg[256];
int sendSOCKET,receiveSOCKET, bytes_received;
char send_data[1024], recv_data[1024];
struct hostent *host;
struct sockaddr_in send_addr, receive_addr, addr;
//bind to port on local for receiving
if((receiveSOCKET = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP))< 0)
{
error("Socket");
exit(1);
}
receive_addr.sin_family = AF_INET;
receive_addr.sin_port = htons(5000);
receive_addr.sin_addr.s_addr = INADDR_ANY; // local ip address
bzero(&(receive_addr.sin_zero),
;
if(bind(receiveSOCKET, (struct sockaddr *)&receive_addr, sizeof(struct sockaddr)) <0)
{
error("unable to bind");
exit(1);
}
// using thread to handle the receiving thing
rc= pthread_create(&thread, NULL,Recv_Handle , (void *)&receiveSOCKET);
//bind to port on local for sending stuff
if((sendSOCKET = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))<0)
{
error("Socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(5004);
addr.sin_addr.s_addr = INADDR_ANY; // LIOCAL IP ADDRESS
bzero(&(addr.sin_zero),
;
if(bind(sendSOCKET, (struct sockaddr *)&addr, sizeof(struct sockaddr)) <0)
{
error("unable to bind");
exit(1);
}
//write to itself
len = sizeof(receive_addr);
n = sendto(sendSOCKET, "got ur message\n", 17, 0, (struct sockaddr *)&receive_addr, len);
if(n<0)error("sendto");
while(1)
{
// get an ip address from the stdin
fgets(msg,256, stdin);// get line from the keyboard containikgn an ip address
send_addr.sin_family = AF_INET;
send_addr.sin_port = htons(5000);
send_addr.sin_addr.s_addr = inet_addr(msg); // use it to make destination address
bzero(&(send_addr.sin_zero),
;
n = sendto(sendSOCKET,"hey wz. up? \n ", 17, 0, (struct sockaddr *)&send_addr, sizeof(struct sockaddr));
}
close(sendSOCKET);
close(receiveSOCKET);
pthread_exit(NULL);
return 0;
}
void *Recv_Handle(void *received)
{
int rec;
int n;
char buf[1024];
rec = *((int*)received);
struct sockaddr_in from_addr;
int Size;
while(1)
{
Size = sizeof(from_addr);
//read
if((n = recvfrom(rec, buf, 1024, 0, (struct sockaddr *)&from_addr, &Size))<0)
{
error("recvfrom");
exit(1);
}
//write
if((n = sendto(rec, "got ur message]\n", 17, 0, (struct sockaddr*)&from_addr, sizeof(struct sockaddr)))<0)
{
error("sendto");
exit(1);
}
}
pthread_exit(NULL);
}
- 01-11-2010 #2
you would do well to post code inside of code tags
here is a wikipedia article detailing seg faults Segmentation fault - Wikipedia, the free encyclopedia
I would advise you to either get comfortable with gdb, or put in printf statements for debugging purposes to find where it occurs
- 01-11-2010 #3Just Joined!
- Join Date
- Jan 2010
- Posts
- 5
thanks ..'ll try that


Reply With Quote