UDP Socket Programming - Server listening on Port 5000 - Client listening on Port 6000
recvfrom() from one Port - 5000
sendto() to other Port - 6000 to the same client.
Server.c
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket() and bind() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define MAXSIZE 255 /* Longest string */
#define SRC_PORT 5000
#define DST_PORT 6000
int main(int argc, char *argv[])
{
int sock; /* Socket */
struct sockaddr_in ServAddr; /* Local address */
struct sockaddr_in ClntAddr; /* Client address */
socklen_t CliAddrLen; /* Length of incoming message */
char recBuffer[MAXSIZE]; /* Buffer for echo string */
int recvMsgSize; /* Size of received message */
int i;
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Construct local address structure */
memset(&ServAddr, 0, sizeof(ServAddr)); /* Zero out structure */
ServAddr.sin_family = AF_INET; /* Internet address family */
ServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
ServAddr.sin_port = htons(SRC_PORT); /* Local port */
/* Construct address structure */
memset(&ClntAddr, 0, sizeof(ClntAddr)); /* Zero out structure */
ClntAddr.sin_family = AF_INET; /* Internet address family */
ClntAddr.sin_addr.s_addr = inet_addr("192.168.1.*");
ClntAddr.sin_port = htons(DST_PORT);
/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0)
DieWithError("bind() failed");
for (;;) /* Run forever */
{
printf("Listening on UDP:5000 \n");
/* Set the size of the in-out parameter */
cliAddrLen = sizeof(ClntAddr);
/* Block until receive message from a client */
if ((recvMsgSize = recvfrom(sock, recBuffer, MAXSIZE, 0,(struct sockaddr *) &ClntAddr, &cliAddrLen)) < 0)
DieWithError("recvfrom() failed") ;
printf("Handling Client: %s\n", inet_ntoa(ClntAddr.sin_addr));
printf("Received Data: %s",recBuffer);
printf("\n");
/* Send response datagram back to the client */
if (sendto(sock, recBuffer, MAXSIZE, 0, (struct sockaddr *) & ClntAddr, sizeof(ClntAddr)) < 0)
DieWithError("sendto() failed");
}
/* NOT REACHED */
}
It works perfectly. Client and Server programming using different ports for sending and receiving the data.
Thank you
Regards
this is wrong
lntAddr.sin_addr.s_addr = inet_addr("CLIENT_IP");
inet_addr takes a dotted ip address not a host name
Related
I am writing a program where the client sends the server a time request. I also want to send my name and get the server to echo it back. So far the program will echo my name but then I get a Segmentation fault (core dumped) where the time should be display. I have attached the code for the clien and server and also a screenshot of the terminal.
SERVER CODE
#include <stdio.h> /* I/O functions */
#include <string.h> /* string functions */
#include <stdlib.h> /* C standard functions */
#include <sys/socket.h> /* socket functions */
#include <sys/types.h> /* library of basic types */
#include <netinet/in.h> /* library of Internet address functions */
#include <arpa/inet.h> /* Internet operations */
#include <time.h> /* time functions */
#define PORT 8080 /* server port # */
#define BUFFSIZE 200 /* buffer size */
int main()
{
int sockfd;
int addrlen;
char buffer[BUFFSIZE];
struct sockaddr_in server;
struct sockaddr_in client;
time_t current_time;
/* Populate socket data structures with IP address and port number */
memset((char *) &server, 0, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
/* Create a UDP socket; returns -1 on failure */
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if (sockfd == -1) {
printf("Socket error\n");
exit(1); /* Exit on error */
}
/* Bind the socket address */
if ((bind(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr_in))) == -1) {
printf("Server bind error\n");
exit (1); /* Exit on error */
}
/* Status message */
printf("The server is listening on port: %d\n", PORT);
printf("Waiting for client request...\n");
printf("Press CTRL + C to exit\n");
while(1) {
addrlen = sizeof(struct sockaddr_in);
recvfrom(sockfd, buffer,BUFFSIZE, 0,(struct sockaddr *)&client, (socklen_t *)&addrlen);
current_time = time(NULL);
memcpy(buffer + strlen(buffer) + 1, ¤t_time, sizeof(current_time));
sendto(sockfd, buffer, strlen(buffer) + 1 + sizeof(current_time), 0, (struct sockaddr *)&client, addrlen);
}
exit(0);
} /* End of time server program */
CLIENT CODE
#include <stdio.h> /* I/O functions */
#include <string.h> /* string functions */
#include <stdlib.h> /* C standard functions */
#include <sys/socket.h> /* socket functions */
#include <sys/types.h> /* library of basic types */
#include <netinet/in.h> /* library of Internet address functions */
#include <arpa/inet.h> /* Internet operations */
#include <time.h> /* time functions */
#define BUFFSIZE 200 /* buffer size */
int main(int argc, char *argv[])
{
int sockfd;
int addrlen;
char buffer[BUFFSIZE] = "GET TIME\r\n";
struct sockaddr_in server;
struct sockaddr_in client;
char *servIP = argv[1]; // Server IP address from command line
int servPort = atoi(argv[2]); // Server port number from command line
char *name = argv[3];
time_t current_time;
/* Check that two arguments were passed on the command line */
if (argc != 4) {
printf("Usage: udp-time-client [IP address] [Port] [Name] \n");
exit(1);
}
/* Populate server socket data structure with IP address and port number */
memset((char *) &server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(servPort);
server.sin_addr.s_addr = inet_addr(servIP);
/* Populate client socket data structure with IP address and port number */
memset((void *)&client, '\0', sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(servPort);
client.sin_addr.s_addr = inet_addr(servIP);
/* Create a UDP socket; returns -1 on failure */
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if (sockfd == -1) {
printf("Socket error\n");
exit(1);
}
/* Status message */
printf("Client is sending on IP address %s port: %d\n", servIP, servPort);
/* Send the time request to the server */
addrlen = sizeof(struct sockaddr_in);
strcpy(buffer, name);
sendto(sockfd, buffer, (int)strlen(buffer) + 1, 0, (struct sockaddr *)&server, addrlen);
printf("Request sent to server\n");
/* Receive the time request from server */
recvfrom(sockfd, (char *) buffer, sizeof(buffer), 0, (struct sockaddr *)&server, (socklen_t *)&addrlen);
/* Print the name received from the server */
printf("\n The name received from the server:%s\n", buffer);
memcpy((void *)current_time, buffer + strlen(buffer) + 1, sizeof(current_time));
/* Print the time received from the server */
printf("\n The time received from the server:%s\n", ctime(¤t_time));
exit(0);
} /* End of time client program */
Screenshot of terminal As you can see, my name is echoed back but I get the error where the time should be
memcpy((void *)current_time, ...);
should be
memcpy(¤t_time, ...);
This pertains to attempting to validate the return address in recvfrom() (the fifth argument to the function) in this UDP echo client:
While I can send data to the server and receive return communications correctly, I'm having trouble validating the return IP address when comparing the fromAddr.sin_addr and echoServAddr.sin_addr.
The goal here is to compare the address in the structure that was used in sendto() and the address in the structure returned from recvfrom() to validate the echo reply from the server indeed came from where the initial transmission from the client was sent (A rudimentary POC that there wasn't a Man In The Middle).
What should I be looking at in order to appropriately validate the return address as it is returned from recvfrom() matches the address transmitted to as referenced in the sendto() call?
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define ECHOMAX 255 /* Longest string to echo */
void DieWithError(char *errorMessage); /* External error handling function */
int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
struct sockaddr_in fromAddr; /* Source address of echo */
unsigned short echoServPort; /* Echo server port */
unsigned int fromSize; /* In-out of address size for recvfrom() */
char *servIP; /* IP address of server */
char *echoString; /* String to send to echo server */
char echoBuffer[ECHOMAX+1]; /* Buffer for receiving echoed string */
int echoStringLen; /* Length of string to echo */
int respStringLen; /* Length of received response */
// manage the command line arguments and errors
if ((argc < 3) || (argc > 4)) {
fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n",
argv[0]);
exit(1);
}
// load servIP
servIP = argv[1];
// load echoString
echoString = argv[2];
// check echoString and error if too long
echoStringLen = strlen(echoString);
if (!(echoStringLen <= 255)) {
DieWithError("BUFFER EXCEEDED ERROR");
}
// load port
if (argc == 4)
echoServPort = atoi(argv[3]);
else
echoServPort = 7;
// create the socket
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
DieWithError("SOCKET CREATION ERROR");
}
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet addr family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
// send the string
inet_pton(AF_INET, servIP, &echoServAddr.sin_addr);
if ((sendto(sock, echoString, strlen(echoString), 0, (struct sockaddr *)
&echoServAddr, sizeof(echoServAddr))) < 0) {
DieWithError("SEND ERROR");
}
// recieve a response
respStringLen = recvfrom(sock, echoBuffer, sizeof(echoBuffer), 0,
(struct sockaddr *) &fromAddr, &fromSize);
if (respStringLen < 0) {
DieWithError("RECV ERROR");
}
// print the message sent
printf("SENT FROM CLIENT: '%s'\n", echoString);
// print the message recieved
echoBuffer[respStringLen] = '\0';
printf("RECEIVED FROM SERVER: '%s'\n", echoBuffer);
// check if from the correct server
if ((struct sockaddr *) &echoServAddr.sin_addr != (struct sockaddr *) &fromAddr.sin_addr) {
DieWithError("INVALID RETURN ADDRESS");
}
// close the socket
close(sock);
// exit the program
exit(0);
}
If the source IP/port of the incoming packet is the same as the destation IP/port of the packet you sent, then the sin_addr and sin_port fields of echoServAddr and fromAddr should match.
This line of code however doesn't do that:
if ((struct sockaddr *) &echoServAddr.sin_addr != (struct sockaddr *) &fromAddr.sin_addr) {
This is comparing the address of echoServAddr.sin_addr against the address of fromAddr.sin_addr. Because these are two separate variables, this will always be false. You instead want:
if ((echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr) ||
(echoServAddr.sin_port != fromAddr.sin_port))
How would I go about making this udpclient asynchronous using pthreads? I want to make sure UDP datagram won't be lost and also don't want the client program to wait forever and not be able to send any more messages
/*udpclient.c program */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN
#include <winsock.h>
#include <windows.h>
#endif
#ifndef WIN
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#endif
/* Here are some details of the sockaddr_in structure and the sockaddr structure
These declarations are copied from winsock.h
struct in_addr { this struct holds a 32 bit IP address
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
#define s_addr S_un.S_addr
struct sockaddr_in { notice this structure is 16 bytes long
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct sockaddr { this generic address structure is 16 bytes long, too!
u_short sa_family;
char sa_data[14];
};
*/
/* we have to send on the same port the server is listening on */
#define PORT 20009
/* simple upd client */
int main()
{
#ifdef WIN
SOCKET sock;
#else
int sock;
#endif
int size;
int nbytes, flags;
int i;
char * cp;
#ifdef WIN
WSADATA wsaData;
int nCode;
#endif
char buffer[100];
char str_addr[20]; /* holds the chars of an IP address */
struct sockaddr_in target_pc, me;
/* magic call to initialize the network I/O code - only Microsoft requires this */
#ifdef WIN
if((nCode = WSAStartup(MAKEWORD(1,1), &wsaData)) != 0){
printf("Opps! WSA error %d\n",nCode);
return -1;
}
#endif
/* create a socket to send on */
sock = socket(PF_INET,SOCK_DGRAM,0);
if(sock < 0) {
printf("socket error = %d\n", sock);
return -1;
}
/* we fill in the address family and port, but we do not know the destination IP address yet */
target_pc.sin_family = PF_INET;
target_pc.sin_port = htons(PORT);
/* fill in my address and port */
me.sin_family = PF_INET;
me.sin_port = htons(0);
me.sin_addr.s_addr = htonl(INADDR_ANY);
i = bind(sock, (struct sockaddr *) &me, sizeof(me));
if( i < 0) {
printf("bind result: %d\n", i);
return -1;
}
nbytes = 99;
while(1){
printf("Enter the target IP address: ");
cp = fgets(str_addr,19,stdin);
/* remove the \n */
str_addr[strlen(str_addr)-1] = '\0';
/* the inet_addr function converts a string form of IP address to a 32 binary integer */
target_pc.sin_addr.s_addr = inet_addr(&str_addr[0]);
printf("Enter your message: ");
cp = fgets(buffer,99,stdin);
/* get the string length so we send exactly this many characters */
nbytes = strlen(buffer);
flags = 0;
size = sendto(sock, (char *) buffer, nbytes,flags,(struct sockaddr *)&target_pc,sizeof(target_pc));
printf("msg size = %d size = %d\n", nbytes, size);
//added
int addrlen = sizeof(target_pc);
size = recvfrom(sock, buffer, nbytes, flags, (struct sockaddr *)&target_pc,&addrlen);
if((size > 0) && (size < 99)){
buffer[size] = '\0'; //add the null byte so buffer now holds a string
i = puts((char *) buffer); // write this string to the display
}
}
#ifdef WIN
system("PAUSE");
#endif
return 0;
}
/udpserver.c program/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifdef WIN
#include <winsock.h>
#include <windows.h>
#endif
#ifndef WIN
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#endif
#define PORT 20009
/* simple upd server
this program receives short messages (<99 characters) from any IP address
and writes them to the display
be sure to use the linker line option "-l wsock32"
*/
int main()
{
/* first define a socket
a socket is an I/O port like a file descriptor
*/
#ifdef WIN
SOCKET sock; /* SOCKET is a typedef for a structure */
#else
int sock;
#endif
int size;
int nbytes, flags;
#ifdef WIN
int addrlen;
#else
socklen_t addrlen;
#endif
int i;
/* char loopback[20]="127.0.0.1"; */
#ifdef WIN
WSADATA wsaData; /* This is struct holds Windows required data */
int nCode;
#endif
char buffer[100];
struct sockaddr_in server; /* this holds my IP address and port info */
struct sockaddr_in from; /* this holds the same info for the sender of the packet
I received */
/* the call to WSAStartup is Windows magic */
#ifdef WIN
if((nCode = WSAStartup(MAKEWORD(1,1), &wsaData)) != 0){
printf("Opps! WSA error %d\n",nCode);
exit;
}
#endif
/* create a socket called sock. It is a datagram socket */
sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock < 0){
printf("socket error = %d\n", sock);
return -1;
}
server.sin_family = AF_INET; /* initialize the server address family */
server.sin_addr.s_addr = htonl(INADDR_ANY); /* notice this struct within a struct */
/* printf("%x\n",server.sin_addr.s_addr); */
server.sin_port = htons(PORT);
/* associate the socket with the address structure - this is called binding */
i = bind(sock, (struct sockaddr *) &server, sizeof(server));
if( i < 0) {
printf("bind result: %d\n", i);
return -1;
} else
printf("Simple UDP server is ready!\n\n");
nbytes = 99; /* receive packets up to 99 bytes long */
flags = 0; /* must be zero or this will not work! */
while(1){
/* the recvfrom function is a read and the arguments are:
sock - the socket we are reading
buffer - array into which to read the data
nbytes - read up to this many bytes
flags - used for special purposes - not needed here
from - sockaddr struct to hold the IP address and port of the sender of the packet
addrlen - the size of the sockaddr struct written by this function
*/
addrlen = sizeof(from);
size = recvfrom(sock, buffer, nbytes, flags, (struct sockaddr *)&from, &addrlen);
if((size > 0) && (size < 99)){
buffer[size] = '\0'; /* add the null byte so buffer now holds a string */
i = puts((char *) buffer); /* write this string to the display */
}
//echo message back to client
if(sock < 0) {//
printf("socket error = %d\n", sock);//
return -1;//
}//
sendto(sock, buffer, nbytes, flags, (struct sockaddr *)&from,addrlen); //
}
#ifdef WIN
system("PAUSE");
#endif
return 0;
}
We could create two threads: one for the sendto (that waits for the user input) and the other for recvfrom(). Next, we can have the recvrom() use Pthread condvar (by calling pthread_cond_wait() on a condvar and a Pthread mutex) and wait. When the user provides an input, we can sendto (which is not really blocking) and then call pthread_cond_signal() to wake up the other thread.
You could certainly simply this, if you wanted. If your application permits, you could completely skip the pthread_cond_wait() since recvfrom() is anyways a blocking call. So, this way, recvfrom() would block but then it would go out of sync with the send calls. The other option is to use the main() thread for the sendto() thread -- in that case, you would just need one additional thread for the recv calls.
I'm looking forward to achive this: Server recieves from number of clients a string(file name) which he has to take from the folder and return it to the the client by a bufferSize which is defined from the command line. It has to be implemented with UDP communication. i'm familiarized with TCP sockets, but i don't get how can i get a fileDescriptor for the udp connection since accepts is missing.
So I'm thinking about this: after the config i do a while-loop in the server where i get 'some kind of descriptor' which i'll send to a new thread which knows where to send the data back... Any ideas?
I've checked the net but didn't found a concrete explanation for this kind of operation.
You don't get connections with UDP; you use sendto() and recvfrom() to send and receive messages.
So the server will call recvfrom() on the socket; unwrap the request from the data it receives, do the appropriate actions, and use sendto() to send the response back to the client from which the request was received.
The client will call sendto() to package a message to the server, and then call recvfrom() to get the response. Note that UDP is an unreliable protocol; there is no guarantee that each message will be delivered. The client has to implement timeouts in case the server dropped a UDP request. Servers have to be able to deal with duplicate requests, too.
I have finally found something helpful, maybe it will be useful for others so here it is: the code is commented and really clear.
PS: sorry for the long paste, but google doesn't let me copy the link.
/*
* udpserver.c - A simple UDP echo server
* usage: udpserver <port>
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSIZE 1024
/*
* error - wrapper for perror
*/
void error(char *msg) {
perror(msg);
exit(1);
}
int main(int argc, char **argv) {
int sockfd; /* socket */
int portno; /* port to listen on */
int clientlen; /* byte size of client's address */
struct sockaddr_in serveraddr; /* server's addr */
struct sockaddr_in clientaddr; /* client addr */
struct hostent *hostp; /* client host info */
char buf[BUFSIZE]; /* message buf */
char *hostaddrp; /* dotted decimal host addr string */
int optval; /* flag value for setsockopt */
int n; /* message byte size */
/*
* check command line arguments
*/
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
portno = atoi(argv[1]);
/*
* socket: create the parent socket
*/
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
/* setsockopt: Handy debugging trick that lets
* us rerun the server immediately after we kill it;
* otherwise we have to wait about 20 secs.
* Eliminates "ERROR on binding: Address already in use" error.
*/
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof(int));
/*
* build the server's Internet address
*/
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)portno);
/*
* bind: associate the parent socket with a port
*/
if (bind(sockfd, (struct sockaddr *) &serveraddr,
sizeof(serveraddr)) < 0)
error("ERROR on binding");
/*
* main loop: wait for a datagram, then echo it
*/
clientlen = sizeof(clientaddr);
while (1) {
/*
* recvfrom: receive a UDP datagram from a client
*/
bzero(buf, BUFSIZE);
n = recvfrom(sockfd, buf, BUFSIZE, 0,
(struct sockaddr *) &clientaddr, &clientlen);
if (n < 0)
error("ERROR in recvfrom");
/*
* gethostbyaddr: determine who sent the datagram
*/
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL)
error("ERROR on gethostbyaddr");
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL)
error("ERROR on inet_ntoa\n");
printf("server received datagram from %s (%s)\n",
hostp->h_name, hostaddrp);
printf("server received %d/%d bytes: %s\n", strlen(buf), n, buf);
/*
* sendto: echo the input back to the client
*/
n = sendto(sockfd, buf, strlen(buf), 0,
(struct sockaddr *) &clientaddr, clientlen);
if (n < 0)
error("ERROR in sendto");
}
}
i have two code here for a UDP chat with a broadcasting mechanism where the server can broadcast a message to all clients simultaneously
Server code...
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(int argc, char *argv[] )
{
struct sockaddr_in client,server;
int s,n;
char b1[100],b2[100];
s=socket(AF_INET,SOCK_DGRAM,0);
server.sin_family=AF_INET;
server.sin_port=htons(2000);
server.sin_addr.s_addr=inet_addr("127.0.0.1");
bind(s,(struct sockaddr *)&server,sizeof(server));
printf("\nServer ready,waiting for client....\n");
n=sizeof(client);
int sock; /* Socket */
struct sockaddr_in broadcastAddr; /* Broadcast address */
char *broadcastIP; /* IP broadcast address */
unsigned short broadcastPort; /* Server port */
char *sendString; /* String to broadcast */
int broadcastPermission; /* Socket opt to set permission to broadcast */
unsigned int sendStringLen; /* Length of string to broadcast */
if (argc < 4) /* Test for correct number of parameters */
{
fprintf(stderr,"Usage: %s <IP Address> <Port> <Send String>\n", argv[0]);
exit(1);
}
broadcastIP = argv[1]; /* First arg: broadcast IP address */
broadcastPort = atoi(argv[2]); /* Second arg: broadcast port */
sendString = argv[3]; /* Third arg: string to broadcast */
/* Create socket for sending/receiving datagrams */
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
broadcastPermission = 1;
while(1)
{
recvfrom(s,b1,sizeof(b1),0,(struct sockaddr *) &client,&n);
if(!(strcmp(b1,"end")))
break;
printf("\nClient:%s",b1);
printf("\nServer:");
gets(b2);
sendto(s,b2,sizeof(b2),0,(struct sockaddr *) &client,n);
broadcastPermission = 1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission));
/* Construct local address structure */
//memset(&broadcastAddr, 0, sizeof(broadcastAddr)); /* Zero out structure */
broadcastAddr.sin_family = AF_INET; /* Internet address family */
broadcastAddr.sin_addr.s_addr = inet_addr(broadcastIP);/* Broadcast IP address */
broadcastAddr.sin_port = htons(broadcastPort); /* Broadcast port */
sendStringLen = strlen(sendString);
for (;;) /* Run forever */
{
/* Broadcast sendString in datagram to clients every 3 seconds*/
sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *) &broadcastAddr, sizeof(broadcastAddr));
// DieWithError("sendto() sent a different number of bytes than expected");
//sleep(3); /* Avoids flooding the network */
}
}
}
Client code...
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define MAXRECVSTRING 255
int main(int argc , char argv[])
{
struct sockaddr_in client,server;
int s,n;
char b1[100],b2[100];
s=socket(AF_INET,SOCK_DGRAM,0);
server.sin_family=AF_INET;
server.sin_port=htons(2000);
server.sin_addr.s_addr=inet_addr("127.0.0.1");
printf("\nClient ready....\n");
n=sizeof(server);
int sock; /* Socket */
struct sockaddr_in broadcastAddr; /* Broadcast Address */
unsigned short broadcastPort; /* Port */
char recvString[MAXRECVSTRING+1]; /* Buffer for received string */
int recvStringLen; /* Length of received string */
if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr,"Usage: %s <Broadcast Port>\n", argv[0]);
exit(1);
}
printf("\nClient ready…11111.\n");
broadcastPort = htons(argv[1]); /* First arg: broadcast port */
printf("\nClient ready….1\n");
/* Create a best-effort datagram socket using UDP */
if(sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)<0)
printf("no sock created");
printf("\nClient ready….2\n");
while(1)
{
printf("\nClient:");
gets(b2);
sendto(s,b2,sizeof(b2),0,(struct sockaddr *) &server,n);
if(strcmp(b2,"end")==0)
break;
recvfrom(s,b1,sizeof(b1),0,NULL,NULL);
printf("\nServer:%s",b1);
broadcastAddr.sin_family = AF_INET; /* Internet address family */
broadcastAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
broadcastAddr.sin_port = htons(broadcastPort); /* Broadcast port */
/* Bind to the broadcast port */
bind(sock, (struct sockaddr *) &broadcastAddr, sizeof(broadcastAddr));
/* Receive a single datagram from the server */
recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0);
recvString[recvStringLen] = '\0';
printf("Received: %s\n", recvString); /* Print the received string */
}
close(sock);
}
These code run without any error but when i send a string from client to the server the server is unable to receive the string and vice versa.
If someone could help me out as to why this is happening.
i am running both the server and client on the same system in different terminal windows..hence the local host address
Some platforms, e.g. Windows, won't receive broadcasts unless the socket is bound to INADDR_ANY, i.e. 0.0.0.0.
To start with:
server.sin_port=2000;
You forget to make this network-order.
In your server code:
The socket is not bound to any IP address. Although it broadcasts the message and the client receives; the vice-versa won't work.
Thus, bind your server and client side sockets to 2 different IP addresses.
When the server broadcasts the message, use appropriate broadcast address (see the subnet of the server-client IP) in the sendto function.