C socket - UDP broadcasting, unable to send messages - c

This program is supposed to receive stuff from a local socket (tcp) and broadcast it, and vice verca.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#define BUFFER_SIZE 1024
#define PORT_TCP 12312
#define PORT_UDP 5678
int main() {
int tcp_sock, udp_sock, client_tcp_sock, bytes_received_udp, bytes_received_tcp;
struct sockaddr_in server_tcp, client_tcp, server_udp, client_udp;
socklen_t client_len;
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE); // emptying buffer
// Create TCP socket to talk with python game
tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
if (tcp_sock < 0) {
perror("Error creating TCP socket");
return EXIT_FAILURE;
}
// Create UDP socket for broadcasting
udp_sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
if (udp_sock < 0) {
perror("Error creating UDP socket");
return EXIT_FAILURE;
}
//enable le broadcast ?
socklen_t broadcastEnable = 1;
if(setsockopt(udp_sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)) < 0)
{
perror("Reusing ADDR failed()");
exit(2);
}
// Bind TCP socket to server address
memset((char *) &server_tcp, 0, sizeof(server_tcp));
server_tcp.sin_family = AF_INET;
server_tcp.sin_port = htons(PORT_TCP);
server_tcp.sin_addr.s_addr = INADDR_ANY;
if (bind(tcp_sock, (struct sockaddr *) &server_tcp, sizeof(server_tcp)) < 0) {
perror("Error binding TCP socket to server address");
return EXIT_FAILURE;
}
// Bind UDP socket to server address
memset((char *) &server_udp, 0, sizeof(server_udp));
server_udp.sin_family = AF_INET;
server_udp.sin_port = htons(PORT_UDP);
server_udp.sin_addr.s_addr = INADDR_ANY;
if (bind(udp_sock, (struct sockaddr *) &server_udp, sizeof(server_udp)) < 0) {
perror("Error binding UDP socket to server address");
return EXIT_FAILURE;
}
// Listen for incoming connections on TCP socket
if (listen(tcp_sock, 5) < 0) {
perror("Error listening for incoming connections on TCP socket");
return EXIT_FAILURE;
} else {
printf("Listening for incoming connections on TCP socket (port %i).\n", PORT_TCP);
}
// Accept incoming connection on TCP socket
client_len = sizeof(client_tcp);
client_tcp_sock = accept(tcp_sock, (struct sockaddr *) &client_tcp, &client_len);
if (client_tcp_sock < 0) {
perror("Error accepting incoming connection on TCP socket");
return EXIT_FAILURE;
} else {
printf("Accepted connection on TCP socket !\n");
}
while (1) {
// Receive data from UDP socket and send to TCP socket
bytes_received_udp = recvfrom(udp_sock, buffer, sizeof(buffer), 0, (struct sockaddr *) &client_udp, &client_len);
if (bytes_received_udp > 0) {
printf("Data received from UDP : %s\n", buffer);
if (send(client_tcp_sock, buffer, bytes_received_udp, 0) < 0) {
perror("Error sending data to TCP socket");
return EXIT_FAILURE;
} else {
printf("Sent data to TCP socket.\n");
//memset(buffer, 0, BUFFER_SIZE); // emptying buffer
}
}
// Receive data from TCP socket and broadcast via UDP socket
bytes_received_tcp = recv(client_tcp_sock, buffer, sizeof(buffer), MSG_DONTWAIT);
if (bytes_received_tcp == 0) { // connection closed
printf("TCP connection closed !\n");
close(client_tcp_sock);
return EXIT_FAILURE;
} else if (bytes_received_tcp > 0) {
printf("Data received from TCP : %s\n", buffer);
if (sendto(udp_sock, buffer, bytes_received_tcp, 0, (struct sockaddr *) &server_udp, sizeof(server_udp)) < 0) {
perror("Error sending data to UDP socket");
return EXIT_FAILURE;
} else {
printf("Sent data to UDP socket.\n");
//memset(buffer, 0, BUFFER_SIZE); // emptying buffer
}
}
}
// Close sockets
close(tcp_sock);
close(udp_sock);
return EXIT_SUCCESS;
}
When I test it with netcat (nc localhost 12312), it looks like it is sending the messages (as I receive them back), but they aren't really broadcasted (tested with netcat too and not received on other machine).
The other way works fine (nc -bu 255.255.255.255 5678), messages are sent to the local socket, even on another machine.
What am I doing wrong ?

The UDP socket is not sending broadcast packets because you're not sending to a broadcast address.
The SO_BROADCAST option doesn't automatically make all outgoing packets broadcast. It just enables you to send broadcast.
If you want to send to a broadcast address, you need to do so explicitly.
struct sockaddr_in udp_broadcast;
memset(&udp_broadcast, 0, sizeof(udp_broadcast));
udp_broadcast.sin_family = AF_INET;
udp_broadcast.sin_port = htons(PORT_UDP);
udp_broadcast.sin_addr.s_addr = INADDR_BROADCAST; // same as 255.255.255.255
...
if (sendto(udp_sock, buffer, bytes_received_tcp, 0,
(struct sockaddr *) &udp_broadcast, sizeof(udp_broadcast)) < 0) {
...

Related

Why do tcp applications send a reset segment after close()ing the connection with data unread in the tcp receive buffer?

I've noticed that whenever I call close() from a TCP application and unread data is still inside the TCP receiving buffer the application sends a RST segment.
1)Is this the default behaviour of any TCP protocol or is OS dependent ? (I'm working on Mac Os);
2)How can I prevent the application from sending a RST segment and allow it to send a FIN even with unread data in the buffer ? Is there any way to flush this data and make the app think it has read anything?
3)Why did tcp engineers decide to implement this behaviour ?
Here is the client and server code respectively. I let the client close the connection with the '#' character still in the TCP receiving buffer on purpose :
int main(){
int sockfd;
struct sockaddr_in servaddr, cli;
//CREATE SOCKET
sockfd = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
//Server ip and port
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr.s_addr);
servaddr.sin_port = htons(PORT_SERVER);
if (connect(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
if(errno == ECONNREFUSED){
printf("Host service inactive on the specified port \n");
}
else if(errno == ETIMEDOUT || errno == EHOSTUNREACH){
printf("Connection timed out ! The server could not be reached \n");
}else{
printf("Connection could not be established \n");
}
errno = 0;
close(sockfd);
return 0;
}
char buff;
while((read(sockfd,&buff,sizeof(buff))) > 0){
if(buff =='\n'){
write(1,&buff,sizeof(buff));
break;
}
write(1,&buff,sizeof(buff));
};
close(sockfd);
return 0;
}
int main(){
int sockfd, connfd;
unsigned int len;
struct sockaddr_in servaddr, cli;
//SOCKET CREATION
sockfd = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr.s_addr);
servaddr.sin_port = htons(PORT);
if ((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
if ((listen(sockfd,128)) != 0) {
printf("Listen failed...\n");
exit(0);
}
for(;;){
len = sizeof(cli);
connfd = accept(sockfd,(struct sockaddr *)&cli, &len);
if (connfd < 0) {
printf("server accept failed...\n");
exit(0);
}
char hello[7]={'H','e','l','l','o','\n','#'};
write(connfd,hello,sizeof(hello));
char buffr;
int r;
while( (r=read(connfd,&buffr,sizeof(buffr))) > 0 ){}
close(connfd);
};
return 0;
}
To send a FIN use shutdown(connfd, SOCK_WR). Then, you keep reading until there is nothing left to read (read or recv returns 0), which means the other computer also sent a FIN. Then, you close the socket.
To my knowledge it is like this on all operating systems. RST is sent if another computer sends a packet to a nonexistent socket. By sending an RST when you close the socket, even if another packet wasn't received yet, your computer is being very slightly more polite by telling the other computer it's not listening any more.

socket binding failed (address already in use, even with SO_REUSEADDR)

I am working in a simple socket project. I would like to know:
why error messages appear before telnet localhost 5678?
why SO_REUSEADDR (between socket() and bind()) don't work, and what else I should try?
Code Output Message:
bind error
Error opening file: Address already in use
telnet localhost 5678
[+]Server Socket is created.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#define BUFSIZE 1024 // Buffer Size
#define PORT 5678
int main() {
printf("telnet localhost 5678\n");
int rfd; // socket descriptor
int clientfd; // client descriptor
struct sockaddr_in client; // Client Socket address
socklen_t client_len; // Length of Client Data
char input[BUFSIZE]; // Client Data -> Server
int bytes_read; // Client Bytes
// 1. socket() = create a socket, SOCK_STREAM = TCP
rfd = socket(AF_INET, SOCK_STREAM, 0);
if (rfd < 0) {
fprintf(stderr, "socket error\n");
exit(-1);
}
printf("[+]Server Socket is created.\n");
// optional
int enable = 1;
if (setsockopt(rfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
fprintf(stderr, "setsockopt(SO_REUSEADDR) failed");
//Initialize the server address by the port and IP
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET; // Internet address family: v4 address
server.sin_addr.s_addr = INADDR_ANY; // Server IP address
server.sin_port = htons(PORT); // Server port
// 2. bind() = bind the socket to an address
int brt = bind(rfd, (struct sockaddr *) &server, sizeof(server));
if (brt < 0) {
int errnum;
errnum = errno;
fprintf(stderr, "bind error\n");
fprintf(stderr, "Error opening file: %s\n", strerror(errnum));
exit(-1);
}
printf("[+]Bind to port %d\n", PORT);
// 3. listen() = listen for connections
int lrt = listen(rfd, 50);
if (lrt < 0) {
printf("listen error\n");
exit(-1);
}
if (lrt == 0) {
printf("[+]Listening....\n");
}
// non-stop loop
while (1) {
// 4. accept() = accept a new connection on socket from client
clientfd = accept(rfd, (struct sockaddr *) &client, &client_len);
if (clientfd < 0) {
fprintf(stderr, "accept failed with error %d\n");
exit(-1);
}
printf("Client connected\n");
...
close(clientfd);
printf("Client disconnected\n");
}
close(rfd);
}
I'm assuming that you are using Linux. If you want to rebind to an address, you should use SO_REUSEPORT not SO_REUSEADDR. Name is really misleading. But make sure that you know how it works and whether you really want to use it or not.
You can check difference here: How do SO_REUSEADDR and SO_REUSEPORT differ?

Attempting to create a basic socket connection in C and running into an infinite loop on accept()

I have an assignment where I need to create a simple HTTP server to handle GET requests and return info from a directory in the directory holding the executable for this code. I am trying to establish a connection between the sockets before ironing out the HTTP requests. However, when I try to connect the client to the server using accept() it triggers an infinite loop with gdb displaying this message:
../sysdeps/unix/sysv/linux/accept.c:26
26 ../sysdeps/unix/sysv/linux/accept.c: No such file or directory.
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
int main(int argc, char* argv[]){
if(argc>1){
perror("Error there should be no command line arguments");
exit(0);
}
int sockfd = 0;
int clientfd = 0;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0){ //create socket and check for error
perror("Error in socket creation");
exit(0);
}
//create sockaddr object to hold info about the socket
struct sockaddr_in server, client;
server.sin_family = AF_INET;
server.sin_port = 0;
server.sin_addr.s_addr = htonl(INADDR_ANY);
socklen_t sockSize = sizeof(server);
//Bind the socket to a physical address exit if there is an error
if((bind(sockfd, (struct sockaddr*)&server, sockSize))<0){
perror("Error binding socket");
exit(0);
}
//Check server details
printf("-------Server Details----------\n");
printf("Port number %d | IP ADDRESS %d\n", ntohs(server.sin_port), (getsockname(sockfd, (struct sockaddr*)&server, &sockSize)));
if((getsockname(sockfd, (struct sockaddr*)&server, &sockSize)) <0){
perror("There is an error in the sock");
exit(0);
}
if(listen(sockfd, 5) <0){
perror("Error switching socket to listen");
exit(0);
}
while((clientfd = accept(sockfd, (struct sockaddr*)&client, (socklen_t*)&sockSize))){
printf("Socket is awaiting connections");
}
// figure out how to setup client to accept and submit HTTP requests
close(sockfd);
return 0;
}
accept() returns -1 on failure. An if will treat any non-zero value as a true condition.
Your loop should look more like the following:
// setup listening socket...
printf("Socket is awaiting connections");
while (1) {
sockSize = sizeof(client); // <-- add this
if ((clientfd = accept(sockfd, (struct sockaddr*)&client, (socklen_t*)&sockSize)) < 0) {
if (errno != EINTR) {
// fatal error, bail out...
break;
}
continue; // retry...
}
printf("Client connected");
// use clientfd to read HTTP request and send HTTP response...
close(clientfd);
}

TCP server that listen on two different ports

I have a TCP server that is listening on two different ports . I created two different sockets one on port 8888 and one on port 6634. I listen on those ports and then i add the two sockets in FD_SET and pass them to select() function ...
When a socket is ready to read i check with FD_ISSET to see on which port i have message to read .
any way when i connect to port 8888 the conception is successful and i can send towards the server and receive ... when i ctrl+c the client the select function is returning again 1 and now my accept() fails ...
when i do the same thing on port 6634 everything is ok... the code stops at the select() and waits for a socket to be ready to read!
can anyone tell me WHY is this happening ?
take a look at my code in attachment
int main()
{
SOCKET conn_request_skt; /* socket where connections are accepted */
char buf[RBUFLEN], buf1[RBUFLEN]; /* reception buffer */
uint16_t lport_n, lport_h, lport_n1, lport_h1; /* port where the server listens (net/host byte ord resp.) */
int bklog = 2; /* listen backlog */
SOCKET s,s1;
int result, n;
socklen_t addrlen;
struct sockaddr_in saddr, caddr; /* server and client address structures */
int optval,childpid,i; /* flag value for setsockopt */
int connectcnt; /* number of connection requests */
fd_set readfds;
/* Initialize socket API if needed */
SockStartup();
/* input server port number */
lport_h=6634;
lport_n = htons(lport_h);
lport_h1=8888;
lport_n1 = htons(lport_h1);
/* create the socket */
printf("Creating first socket\n");
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
err_fatal("socket() failed");
printf("done, socket number %u\n",s);
/* bind the socket to any local IP address */
saddr.sin_family = AF_INET;
saddr.sin_port = lport_n;
saddr.sin_addr.s_addr = INADDR_ANY;
showAddr("Binding to address first socket", &saddr);
result = bind(s, (struct sockaddr *) &saddr, sizeof(saddr));
if (result == -1)
err_fatal("bind() failed");
printf("done.\n");
printf("Creating second socket\n");
s1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s1 == INVALID_SOCKET)
err_fatal("socket() failed");
printf("done, socket number %u\n",s1);
/* bind the socket to any local IP address */
saddr.sin_port=lport_n1;
showAddr("Binding to address second socket", &saddr);
result = bind(s1, (struct sockaddr *) &saddr, sizeof(saddr));
if (result == -1)
err_fatal("bind() failed");
printf("done.\n");
/* listen */
printf ("Listening at socket %d with backlog = %d \n",s,bklog);
result = listen(s, bklog);
if (result == -1)
err_fatal("listen() failed");
printf("done.\n");
printf ("Listening at socket %d with backlog = %d \n",s1,bklog);
result = listen(s1, bklog);
if (result == -1)
err_fatal("listen() failed");
printf("done.\n");
for (;;)
{
FD_ZERO(&readfds); /* initialize the fd set */
FD_SET(s, &readfds);
FD_SET(s1, &readfds); /* add socket fd */
printf("here \n");
printf("result bifore select is %d \n", result);
result=select(s1+1, &readfds, 0, 0, 0);
printf("result after select is %d \n", result);
if(result<0)
{
err_fatal("select() failed");
}
if(result>0)
{
if(FD_ISSET(s,&readfds))
{
conn_request_skt=s;
/* accept next connection */
addrlen = sizeof(struct sockaddr_in);
s = accept(conn_request_skt, (struct sockaddr *) &caddr, &addrlen);
if (s == INVALID_SOCKET)
err_fatal("accept() failed");
showAddr("Accepted connection from", &caddr);
printf("new socket: %u\n",s);
/* serve the client on socket s */
for (;;)
{
n=recv(s, buf, RBUFLEN-1, 0);
if (n < 0)
{
printf("Read error\n");
closesocket(s);
printf("Socket %d closed\n", s);
break;
}
else if (n==0)
{
printf("Connection closed by party on socket %d\n",s);
//closesocket(s);
break;
}
else
{
printf("Received line from socket %03d :\n", s);
buf[n]=0;
printf("[%s]\n",buf);
if(writen(s, buf, n) != n)
printf("Write error while replying\n");
else
printf("Reply sent\n");
}
}
}
if(FD_ISSET(s1,&readfds))
{
conn_request_skt=s1;
/* accept next connection */
addrlen = sizeof(struct sockaddr_in);
printf("bifore accept! \n");
s1 = accept(conn_request_skt, (struct sockaddr *) &caddr, &addrlen);
if (s1 == INVALID_SOCKET)
err_fatal("accept() failed");
showAddr("Accepted connection from", &caddr);
printf("new socket: %u\n",s1);
/* serve the client on socket s */
for (;;)
{
n=recv(s1, buf, RBUFLEN-1, 0);
if (n < 0)
{
printf("Read error\n");
closesocket(s1);
printf("Socket %d closed\n", s1);
break;
}
else if (n==0)
{
printf("Connection closed by party on socket %d\n",s1);
//closesocket(s);
break;
}
else
{
printf("Received line from socket %03d :\n", s1);
buf[n]=0;
printf("[%s]\n",buf);
if(writen(s1, buf, n) != n)
printf("Write error while replying\n");
else
printf("Reply sent\n");
}
}
}
}
}
}
The first listener socket is created with:
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
And then the data socket is accepted with:
conn_request_skt=s;
s = accept(conn_request_skt, (struct sockaddr *) &caddr, &addrlen);
See? The next loop, when you are going to select over the listener socket, s no longer holds that socket, but the (closed) data socket.
The solution is to use different variables for the listener socket and the data socket (conn_request_skt is just obfuscating the issue).
You are overwriting your socket variable s1 with the result of the accept() call. So s1 contains now the descriptor of the socket you are actually reading from. Then you close that socket. But in the next pass of the main loop, you check for readability on that (now closed) descriptor, which does not work.
I believe it would be better not to reuse variables in this case. Use a new variable for the actual connection socket, so that you retain the original listening socket in s1.

How to reduce message dropping in UDP socket communication, using only single port?

I have writter code snippet for UDP Client and server. I am using same port for sending and receiving. My problem is that there are many messages drops at client side, so can someone help me to optimize my code, here is my code for UDP client:
#define SERVERIP "192.168.170.155"
#define SERVERPORT 5000
#define DEVICE_SEND_PORT 5000
#define DEVICE_RECEIVE_PORT 5000
#define BUFFERSIZE 2048
/**For socket file descriptor identification*/
#define S1READY 0x01
int m_SendSocketId;
int m_ReceiveSocketId;
int msgcount;
int socketbuffsize = 1*1024*1024;
/**
* FUNCTION NAME : waitToRead
* Implementation of select and non-blocking socket mechanism
* #param socket Socket that needs to be in select and non blocking mode
* #return Returnd the file descriptors which, returned by select function
*/
int waitToRead(int socket)
{
fd_set fds;
struct timeval timeout;
int rc; // number of file descriptor returned
int result; // result
int fd; // file descriptor
fd=fcntl(socket,F_GETFL,0);
fcntl(socket,F_SETFL,fd | O_NONBLOCK);
// Set time limit.
timeout.tv_sec = 1;
timeout.tv_usec = 0;
// Create a descriptor containing our sockets.
FD_ZERO(&fds);
FD_SET(socket, &fds);
rc = select(sizeof(fds)*8, &fds, NULL, NULL, &timeout);
if (rc==-1)
{
printf("[%s:%d#%s] Select Failed\n",__FILE__, __LINE__,__func__);
return -1;
}
result = 0;
if (rc > 0)
{
if (FD_ISSET(socket, &fds))
result |= S1READY;
}
return result;
}
/**
* FUNCTION NAME : receiveMessage
* This function opens particular port that is defined in the
* Configuration file, and listens on that port.
* #return if there'll be any issue in listening, then it will return
* false otherwise it will return true.
*/
bool receiveMessage()
{
struct sockaddr_in serverAddr; //Information about the Device UDP Server
struct sockaddr_in client_addr; // Information about Qgate Server
char buffer[BUFFERSIZE]; // Buffer to store incoming message
int addr_len; // to store client address length
int serverlen; // to store server address length
int sockResult; // to store result given by waitToRead
int optval = 1;
int receivedByte = 0;
//Open a datagram Socket
if((m_ReceiveSocketId = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("[%s:%d#%s] UDP Client - socket() error\n",__FILE__, __LINE__,__func__);
return false;
}
//Configure Server Address.
//set family and port
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(DEVICE_RECEIVE_PORT);
setsockopt(m_ReceiveSocketId, SOL_SOCKET,SO_REUSEADDR, &optval, sizeof(optval));
/*if (setsockopt(m_ReceiveSocketId, SOL_SOCKET, SO_RCVBUF, &socketbuffsize, sizeof(socketbuffsize)) == -1)
{
printf("Recieve Socket memory Allocation fail\n");
}*/
if((serverAddr.sin_addr.s_addr = INADDR_ANY) == (unsigned long)INADDR_NONE)
{
printf("[%s:%d#%s] Host Not found(%d)\n",__FILE__, __LINE__,__func__,h_errno);
close(m_ReceiveSocketId); // close the socket
return false;
}
if (bind(m_ReceiveSocketId, (struct sockaddr *) &serverAddr,sizeof(struct sockaddr_in)) < 0 )
{
printf("[%s:%d#%s] UDP Client- Socket Bind error=%s\n",__FILE__, __LINE__,__func__,strerror(errno));
close(m_ReceiveSocketId); // close the socket
return false;
}
serverlen = (int )sizeof(serverAddr);
addr_len = sizeof(struct sockaddr);
// Loop and listen for incoming message
while(1)
{
//wait at select to, read
sockResult = waitToRead(m_ReceiveSocketId);
if(sockResult == S1READY)
{
receivedByte = read(m_ReceiveSocketId,buffer,BUFFERSIZE);
buffer[receivedByte] = '\0';
if(receivedByte == -1)
{
printf("[%s:%d#%s] UDP Client - receive error", __FILE__,__LINE__,__func__);
close(m_ReceiveSocketId);
return false;
}
else if(receivedByte > 0)
{
//printf("[%s:%d#%s] received message = %d bytes\n",__FILE__,__LINE__,__func__,(int)strlen(buffer));
printf("count: %d, buffer %s \n", msgcount++, buffer);
}
}
memset(buffer, 0, BUFFERSIZE);
fflush(stdout);
}
close(m_ReceiveSocketId); // close the socket
printf("[%s:%d#%s] Recieve socket closed:%s\n",
__FILE__, __LINE__,__func__, strerror(errno));
return true;
}
bool sendMessage(char *message)
{
struct sockaddr_in serverAddr; //Information about the server
struct sockaddr_in deviceAddr; //Device UDP Client Address for sending message
int optval = 1;
//Open a datagram Socket
if((m_SendSocketId = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("[%s:%d#%s] UDP Client - socket() error\n",__FILE__, __LINE__,__func__);
return false;
}
// Clear out the device struct
memset(&deviceAddr, 0x00, sizeof(struct sockaddr_in));
deviceAddr.sin_family = AF_INET;
deviceAddr.sin_port = htons(DEVICE_SEND_PORT);
setsockopt(m_SendSocketId, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
/*if (setsockopt(m_SendSocketId, SOL_SOCKET, SO_SNDBUF, &socketbuffsize, sizeof(socketbuffsize)) == -1)
{
printf("send Socket memory Allocation fail\n");
}*/
if((deviceAddr.sin_addr.s_addr = INADDR_ANY) == (unsigned long)INADDR_NONE)
{
// in netdb.h
printf("[%s:%d#%s] Host Not found(%d)\n",__FILE__, __LINE__,__func__, h_errno);
close(m_SendSocketId); // close the socket
return false;
}
if (bind(m_SendSocketId, (struct sockaddr *) &deviceAddr,sizeof(struct sockaddr_in)) < 0 )
{
printf("[%s:%d#%s] UDP Client- Socket Bind error=%s\n",__FILE__, __LINE__,__func__,strerror(errno));
close(m_SendSocketId); // close the socket
return false;
}
// Clear out the server struct
memset(&serverAddr, 0x00, sizeof(struct sockaddr_in));
//Configure Server Address.
//set family and port
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVERPORT);
//serverAddr.sin_addr.s_addr = htonl(39898);
if((serverAddr.sin_addr.s_addr = inet_addr(SERVERIP)) == (unsigned long)INADDR_NONE)
{
printf("[%s:%d#%s] Host Not found %d\n",__FILE__, __LINE__,__func__,h_errno);
close(m_SendSocketId);
return false;
}
// Send data to the server.
if( sendto(m_SendSocketId, message,strlen(message) ,0, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0 )
{
printf("[%s:%d#%s] UDP Client - sendto() error=%s \n",__FILE__, __LINE__,__func__,strerror(errno));
close(m_SendSocketId);
return false;
}
close(m_SendSocketId);
return true;
}
int main ()
{
int loop;
char str[10];
msgcount = 1;
pthread_t receiveThread;
if(pthread_create(&receiveThread, NULL,(void *)&receiveMessage, NULL) != 0)
{
printf("[%s:%d#%s] thread create Failed(%s)\n",
__FILE__, __LINE__,__func__, strerror(errno));
return false;
}
for(loop =0; loop < 1000; loop++)
{
sprintf(str,"%4d",loop);
sendMessage(str);
}
pthread_join(receiveThread, NULL);
return 0;
}
Here is the temporary UDP server code, it receives almost above 90% messages and also sends the same, but udpclient is not able to receive the messages.
int main()
{
int sock;
int addr_len, bytes_read, bytes_send;
char recv_data[1024];
int i;
int count=0;
struct sockaddr_in server_addr , client_addr;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
//client_addr.sin_family = AF_INET;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (bind(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
{
perror("Bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
printf("\nUDPServer Waiting for client on port 5000");
fflush(stdout);
while (1)
{
bytes_read = recvfrom(sock,recv_data,1024,0,(struct sockaddr *)&client_addr, (socklen_t *)&addr_len);
recv_data[bytes_read] = '\0';
if(recv_data[0]!=0x07)
{
recv_data[0] = 0x09;
//client_addr.sin_port = htons(51254);
bytes_send = sendto(sock, recv_data, bytes_read, 0, (struct sockaddr *)&client_addr, (socklen_t)sizeof(client_addr));
if(bytes_send < 0 )
{
perror("send to ");
}
printf("\nNumber %d", ++count);
memset(&recv_data, 0x00, 1024);
}
else
{
printf("Received Keep ALive\n");
}
memset(&client_addr, 0x00, sizeof(struct sockaddr_in));
fflush(stdout);
}
return 0;
}
Any help would be highly appreciated.
Thanks Yuvi
Your code has nothing to do with UDP dropping packets, except possibly that you are sending packets too fast for the network or the receiver. UDP isn't reliable. It drops packets. If your application protocol requires no dropped packets, you have to build in reliability at that level, via an ACK-based or NACK-based protocol with retries.
Or use TCP like everybody else does in this situation.
The problem was in sendMessage Function, here I was recreating socket every time when I need to send message, and I think that takes time. I don't know yet which is calls are blocking but making sending socket resolves my problem. Now the dropping of message is upto 20 to 30 % only.

Resources