soheil
09-07-2012, 03:00 PM
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);
}
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);
}