Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-07-2012, 03:00 PM   PM User | #1
soheil
New to the CF scene

 
Join Date: Jun 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
soheil is an unknown quantity at this point
UDP socket programming in C

Hi my friends...

I am writing one udp socket with C language with some specific conditions
1- UDP server should take a Datagram from one client and broadcast it to other clients.
2- Ip and Port of the other clients are stored in one txt file(users.txt) such a way that each line of text file is consisted of <IP> <PORT> e.g.
192.168.10.3 4935
192.168.10.6 4976

... my serve code is below but I have segmentation fault:

any help will be strongly appreciated...thanks a lot in advance.



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h> // needed to know the current date and time
#include "errlib.h"
#include "sockwrap.h"

#define MAXBUFL 255
//#define PORT 1300

char *prog;

struct clients{
char ip[MAXBUFL];
int port;
};


int main(int argc, char *argv[])
{

struct clients cli;
int sockfd, n;
char buf[MAXBUFL+1];
char newBuf[MAXBUFL +1];
FILE * fp;
struct sockaddr_in servaddr, cliaddr;
socklen_t clilen;
socklen_t broadlen;
// time_t ticks;
fp=fopen("users.txt", "rt");
/* for errlib to know the program name */

prog = argv[0];

/* create socket */

sockfd = Socket(AF_INET, SOCK_DGRAM, 0);

// int broadcastEnable=1;

//int ret=setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));

/* if(ret){
printf("could nt open the socket in broadcast mode");
close(sockfd);
return;
}*/

/* specify address to bind to */
memset(newBuf,0,MAXBUFL);
memset(buf,0,MAXBUFL);
memset (&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

//inet_pton(AF_INET, "239.255.255.250", &servaddr.sin_addr); // Set the broadcast IP address

Bind (sockfd, (SA*) &servaddr, sizeof(servaddr));

#ifdef TRACE
printf ("(%s) socket created\n", prog);
#endif

while (1)
{
/* get the request */

#ifdef TRACE
printf ("(%s) waiting for requests ...\n", prog);
#endif
clilen = sizeof(cliaddr);
n = Recvfrom (sockfd, buf, MAXBUFL, 0, (SA*) &cliaddr, &clilen);

#ifdef TRACE
printf ("(%s) - new request from client %s:%u\n", prog, inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
#endif

/* get and format date and time */

// ticks = time (NULL);

// n = snprintf (newBuf,"%s.\n", buf);

// strupr(buf);
strcpy(newBuf,buf);
printf("%d \n",strlen(newBuf));


/* send the answer */
broadlen=sizeof(cli);

char ch;

int i=0;

int ipflag=1;
ch=getc(fp);
while(ch != EOF){
while(ch !='\n'){
while(ch != ' ' || ipflag==1){
cli.ip[i]=ch;
i=i+1;
ch=getc(fp);
}
i=0;
ipflag=0;
cli.port=atoi(ch);
ch=getc(fp);
printf("124");
}

Sendto(sockfd, newBuf, sizeof(newBuf), 0, (SA*) &cli, broadlen);
ipflag=1;
ch=getc(fp);
}



memset(newBuf,0,MAXBUFL);
memset(buf,0,MAXBUFL);
}

exit(0);
}
soheil is offline   Reply With Quote
Old 09-07-2012, 03:25 PM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,224
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
The first thing you should do when you encounter any runtime faults is to run it in a debugger so you can see what is going on.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:47 PM.


Advertisement
Log in to turn off these ads.