How to read data from socket correctly? - c

I've got a simple client/server application. The user writes strings in the console. When he pushes enter string is sent. I can transfer one line correctly, but after it substitutes the first letter of first sent word to each next. For example, if a user sends "Hello", the server will get "Hello", but after, if I send "Hello" again, the server will get "HHello". If I try to clear buffer at the client-side after sending it, it never sends something again.
Server code:
// Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 57174
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
char buffer[1024];
bzero(buffer, sizeof(buffer));
int step = 0;
while(1){
valread = read( new_socket , buffer, 1024);
if(valread == 0)
break;
printf("%s", buffer );
printf("\n");
bzero(buffer, sizeof(buffer));
};
return 0;
}
Client code:
// Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 57174
int main(int argc, char const *argv[])
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[2]));
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
// char buffer[1024] = {0};
unsigned int N = 10, delta=10, i = 0;
char* buf = (char*) malloc (sizeof(char)*N);
while (1) {
buf[i] = getchar();
if(buf[i] == 27)
break;
if(buf[i] == 10){
send(sock , buf , strlen(buf) , 0 );
// bzero(buf, sizeof(buf));
N = 10;
buf = (char*) realloc (buf, sizeof(char)*N);
i = 0;
}
if (++i >= N) {
N += delta;
buf = (char*) realloc (buf, sizeof(char)*N);
}
}
return 0;
}

if a user sends "Hello", the server will get "Hello", but after, if I send "Hello" again, the server will get "HHello"
This is because you missed an else in your client, in
if(buf[i] == 10){
send(sock , buf , strlen(buf) , 0 );
// bzero(buf, sizeof(buf));
N = 10;
buf = (char*) realloc (buf, sizeof(char)*N);
i = 0;
}
if (++i >= N) {
N += delta;
buf = (char*) realloc (buf, sizeof(char)*N);
}
you need to replace
if (++i >= N) {
by
else if (++i >= N) {
else after you sent you buffer and set i to 0 you increment it, and you will memorize the next char at the index 1, the character at the index 0 is still present and you will send it again and again
You also have a problem in your client at
send(sock , buf , strlen(buf) , 0 );
because you do not put a null character in buff needed by strlen to return the expected value, so the behavior is undefined. In fact you do not need strlen, just do
send(sock , buf , i , 0 );
supposing you do not want to send the \n
On your server side
char buffer[1024];
...
valread = read( new_socket , buffer, 1024);
if(valread == 0)
break;
printf("%s", buffer );
you fill each time buffer with null characters but in case you read 1024 characters there is no null character in your buffer and printf will go out of the buffer with an undefined behavior
warning read returns -1 on error, valread == 0 is wrong
remove all your bzero an just do
char buffer[1024];
...
while ((valread = read(new_socket, buffer, sizeof(buffer)-1)) > 0) {
buffer[valread ] = 0;
printf("%s", buffer);
}
notice I used sizeof(buffer) rather than 1024, that allows to be sure to have the right size even you resize buffer
Other remarks for the client :
the variable hello is useless
by definition sizeof(char) values 1, so sizeof(char)*N can be replaced by N everywhere
do not compare the read char with the literal 10 and 27, compare with '\n' and '\e'
you do not manage the EOF in input, for that you need to save the read char in an int rather than a char (like buf[i] is) to compare it with EOF
In the server the variable step is useless
Out of that you use SOCK_STREAM so your socket is a stream (tcp not udp), that means you cannot suppose the size of the data you read each time you call read, I mean if the client sent N bytes that does not mean the server will read N bytes on the corresponding read (if I can say 'corresponding' because there is no correspondence ;-) ).
Supposing the other problems are fixed if you input azeqsd\n you send azeqsd but may be on the server side you will read azeq so print azeq\n and on the next loop you will read sd and print sd\n.
It is also possible the server read a partial or full concatenation of several buffers sent separably by the client.
Do you want that behavior ? if no you need to send the size before each buffer to know how much to read even on several times to constitute the full sent buffer (an other advantage is you no not read byte per

Related

server sends back unwanted characters

#include <io.h>
#include <stdio.h>
#include <winsock2.h>
#include <string.h>
#define MY_PORT 8989
#define MAXBUF 256
int main(int argc, char *argv[]){
WSADATA wsa;
SOCKET sockfd ,clientfd; //define 2 sockets
struct sockaddr_in self;
char buffer[MAXBUF];
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
/*---create streaming socket---*/
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
perror("Socket");
exit(errno);
}
printf("Socket created.\n");
/*---initialize address/port structure---*/
/* bzero(&self, sizeof(self));*/
self.sin_family = AF_INET;
self.sin_port = htons(MY_PORT);
self.sin_addr.s_addr = INADDR_ANY;
/*---assign a port number to the socket---*/
if ((bind(sockfd, (struct sockaddr*)&self, sizeof(self)))!=0){
perror("socket--bind");
exit(errno);
}
puts("Bind done");
/*---make it a "listening socket"---*/
if ((listen(sockfd, 20))!=0){
perror("socket--listen");
exit(errno);
}
puts("Waiting for incoming connections...");
/*---forever... ---*/
while (1){
struct sockaddr_in client_addr;
int addrlen=sizeof(client_addr);
/*---accept a connection (creating a data pipe)---*/
clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
//create variables
int length=recv(clientfd, buffer, MAXBUF, 0);
//output in reverse order & lowercase
for(int i=0; buffer[i]!='\0'; i++){
if(buffer[i]>=65 && buffer[i]<=90){ // check ascii value
buffer[i]= buffer[i]+32; // if uppercase thn change
}
}
int loop=0, len=strlen(buffer);
char temp; //temporary holds character to swap
len--; // remove null and start from 0
while (loop<len){
temp=buffer[loop];
buffer[loop]=buffer[len];
buffer[len]=temp;
loop++;
len--;
}
//send back
send(clientfd, buffer, sizeof(buffer), 0);
/*---close connection---*/
close(clientfd);
}
/*---clean up (should never get here!)---*/
close(sockfd);
WSACleanup();
return 0;
}
The reversing of string from client works well but it also gives out many other unwanted characters behind it and two 'PuTTy's, I'm very new to socket programming so any help is much appreciated. I can't use any string functions other than strlen. Currentlly using puTTy to open server on 8989. Telnet connection is passive.
You code assumes that buffer is NUL terminated, however there is no guaranty that the NUL character will be received by recv (which may be a partial recv and this is streaming socket).
It would make more sense to reverse the data using the return value from recv (if a positive value). This way you do not have to worry about message boundaries.
int length=recv(clientfd, buffer, MAXBUF, 0);
//output in reverse order & lowercase
for(int i=0; i < length; i++){
if(buffer[i]>=65 && buffer[i]<=90){ // check ascii value
buffer[i]= buffer[i]+32; // if uppercase thn change
}
}
You are ALWAYS sending MAXBUF (ie 256) bytes because in
send(clientfd, buffer, sizeof(buffer), 0);
sizeof(buffer) is the size of the char array declared by
#define MAXBUF 256
char buffer[MAXBUF];
Fix this by using strlen(buffer) for example, or keeping the len you got previously.

observing erratic behavior while downloading(get) the file using socket

I have implemented the mget command using socket. But the output i am getting is having some random behavior. Sometime i am able to download the whole file on client, sometimes i am able to download files partially, sometime server code gives segmentation fault and sometimes client goes into infinite loop.
Server Code:
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
// char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
/*bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");*/
char command[512];
bzero(command, 512);
char buffer[4096];
char output[5000];
char f_size[20];
int send_bytes;
int written_bytes;
int total_sent_bytes = 0;
long int file_size;
n = recv(newsockfd, command, 512, 0); //try with MSG_WAITALL
if(n < 0)error("Error receiving command");
puts(command); //only file name
FILE * fp;
fp = fopen(command, "rb");
if(fp == NULL)error("Can not open requested file");
FILE * test_file_fp;
test_file_fp = fopen("test_file.txt", "wb");
if(test_file_fp == NULL)error("Can not open test file");
fseek(fp,0, SEEK_END);
file_size = ftell(fp);
rewind(fp);
sprintf(f_size,"%ld", file_size);
send(newsockfd, f_size, strlen(f_size), 0);
while(!feof(fp)){
bzero(buffer, 4096);
n = fread(buffer, sizeof(char), 4095, fp);
sprintf(output, "read bytes using fread = %d", n);
puts(output);
send_bytes = send(newsockfd, buffer, strlen(buffer) + 1, MSG_MORE);
total_sent_bytes += send_bytes;
sprintf(output, "sent bytes using send = %d", send_bytes);
puts(output);
written_bytes = fwrite(buffer, sizeof(char), strlen(buffer), test_file_fp);
sprintf(output, "written bytes using fwrite = %d", written_bytes);
puts(output);
//bzero(command, 512);
//recv(newsockfd, buffer, 512, 0);
puts("\n");
}
sprintf(output, "total sent bytes using send = %d\n", total_sent_bytes);
puts(output);
send(newsockfd, NULL, 1, 0);
fclose(test_file_fp);
fclose(fp);
close(newsockfd);
close(sockfd);
return 0;
}
Client Code:
#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 <netdb.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
int i;
// char buffer[5000];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
char command[512];
char output[5096];
int send_command_bytes;
int reveived_bytes;
char buffer[4096];
long int total_bytes;
long int total_received_bytes;
int written_bytes;
sprintf(output, "IP Protocol Number = %d", IPPROTO_TCP);
puts(output);
FILE * fp;
fp = fopen("received_file.txt", "wb");
if(fp == NULL)error("Could not open file for receiving data");
memcpy(command, "send_me.txt", 11);
send_command_bytes = send(sockfd, command, strlen(command), 0);
puts(command);
recv(sockfd, buffer, 4096, 0);
total_bytes = atoi(buffer);
sprintf(output, "Total bytes to be received = %ld", total_bytes);
puts(output);
total_received_bytes = 0;
while(totala_received_bytes < total_bytes){
bzero(buffer, 4096);
reveived_bytes = recv(sockfd, buffer, 4095, 0); //try with MSG_WAITALL, MSG_DONTWAIT
total_received_bytes += reveived_bytes;
sprintf(output, "Number of bytes received = %d", reveived_bytes);
puts(output);
written_bytes = fwrite(buffer, sizeof(char), strlen(buffer), fp);
sprintf(output, "Total written bytes = %d", written_bytes);
puts(output);
//send(sockfd, "1", 2, 0);
sprintf(output, "total Number of bytes received so far = %ld", total_received_bytes);
puts(output);
puts("\n");
}
fclose(fp);
close(sockfd);
return 0;
}
From client i am sending the file name "send_me.txt" which needs to be downloaded and this filename is being read by server in a buffer named command. This file is being opened in server and then read and then sent to client.
I have tried many possibilities to overcome this issue but the issue still persists.
Your code has both of the common problems when dealing with communications over sockets
1. Lack of NUL Termination
Strings in C are NUL terminated, so either you need to send the NUL as part of the message, or you need to add the NUL on the receive side.
2. Assumptions about message sizes
It's a common misconception in socket programming that each recv will match the corresponding send. For example, if I send 10 bytes, followed by 50 bytes, and later 20 bytes, then the expectation is that recv will need to be called three times, and will return 10 bytes, 50 bytes, and 20 bytes. That is completely, utterly, totally wrong. In fact, the first recv will return anywhere from 1 byte to 80 bytes. That is, it may return any portion of the data from the first send, up to all of the data from all of the sends.
Example from your code
In the server code, you do this
sprintf(f_size,"%ld", file_size);
send(newsockfd, f_size, strlen(f_size), 0);
If the file_size is 123, then the sprintf will write 1, 2, 3, \0 to f_size, i.e. three digits and a NUL terminator. The strlen will return 3, so the code only sends the three digits but doesn't send the NUL terminator.
On the client side, you have this
recv(sockfd, buffer, 4096, 0);
total_bytes = atoi(buffer);
Since the server doesn't send the NUL byte and the client doesn't add the NUL byte, the atoi will see whatever the recv put in the buffer, followed by garbage. If you're lucky, the first garbage character won't be a digit, and atoi will work correctly. If you're unlucky the first garbage character will be a digit.
In addition to the missing NUL, the recv may only get 1 byte, in which case the file size is 1 (assuming the first garbage character is not a digit). Or the recv may get 100 bytes (the 3 byte length plus some of the bytes from the file). If the file happens to start with digits, the size will be wrong.
Fixing the code
On the server side, send the file size as a fixed length message with the NUL included, e.g.
#define MSGLEN 24
char buffer[MSGLEN];
snprintf( buffer, MSGLEN, "%*ld", MSGLEN-1, file_size );
send( sockfd, buffer, MSGLEN, 0 );
On the client side, read in a loop until you have all of the bytes for the size, but no more
#define MSGLEN 24
char buffer[MSGLEN];
int count = 0;
int index;
for ( index = 0; index < MSGLEN; index += count )
{
count = recv( sockfd, &buffer[index], MSGLEN - index, 0 );
if ( count <= 0 )
break;
}
if ( index != MSGLEN || buffer[MSGLEN-1] != '\0' )
{
printf( "bad file size\n" );
exit( 1 );
}
total_bytes = atoi( buffer );
One final note
while(!feof(fp))-is-always-wrong

Asynchronous C client for a multiclient C server

I have a client which is working fine, but whenever I run a new client, sometimes I don't receive the sent message on the other client already running, while using telnet it works flawlessly, the message "broadcasts" to all connected clients, and I want whenever a message is received to one of the clients to show even if I didn't already send a message.
Should I use select on clients ? and what should be changed ?
client.c:
#include <stdio.h> //printf
#include <string.h> //strlen
#include <sys/socket.h> //socket
#include <arpa/inet.h> //inet_addr
#include <unistd.h>
int main(int argc , char *argv[]){
int sock;
struct sockaddr_in server;
char message[256] , server_reply[256];
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 9034 );
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0){
perror("connect failed. Error");
return 1;
}
puts("Connected\n");
//keep communicating with server
for(;;){
printf("Enter message: ");
memset(message, 0, 256);
fgets(message, 256,stdin);
// scanf("%s" , message);
//Send some data
if( send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive a reply from the server
if( recv(sock , server_reply , 256 , 0) < 0)
{
puts("recv failed");
break;
}
printf("Server Reply: %s\n", server_reply);
server_reply[0]='\0';
}
close(sock);
return 0;
}
server.c:
/*
** selectserver.c -- a cheezy multiperson chat server
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT "9034" // port we're listening on
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main(void){
fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
int fdmax; // maximum file descriptor number
int listener; // listening socket descriptor
int newfd; // newly accept()ed socket descriptor
struct sockaddr_storage remoteaddr; // client address
socklen_t addrlen;
char buf[256]; // buffer for client data
int nbytes;
char remoteIP[INET6_ADDRSTRLEN];
int yes=1; // for setsockopt() SO_REUSEADDR, below
int i, j, rv;
struct addrinfo hints, *ai, *p;
FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds);
// get us a socket and bind it
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL, PORT, &hints, &ai)) != 0) {
fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
exit(1);
}
for(p = ai; p != NULL; p = p->ai_next) {
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener < 0) {
continue;
}
// lose the pesky "address already in use" error message
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) {
close(listener);
continue;
}
break;
}
// if we got here, it means we didn't get bound
if (p == NULL) {
fprintf(stderr, "selectserver: failed to bind\n");
exit(2);
}
freeaddrinfo(ai); // all done with this
// listen
if (listen(listener, 10) == -1) {
perror("listen");
exit(3);
}
// add the listener to the master set
FD_SET(listener, &master);
// keep track of the biggest file descriptor
fdmax = listener; // so far, it's this one
// main loop
for(;;) {
read_fds = master; // copy it
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select");
exit(4);
}
// run through the existing connections looking for data to read
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) { // we got one!!
if (i == listener) {
// handle new connections
addrlen = sizeof remoteaddr;
newfd = accept(listener,
(struct sockaddr *)&remoteaddr,
&addrlen);
if (newfd == -1) {
perror("accept");
} else {
FD_SET(newfd, &master); // add to master set
if (newfd > fdmax) { // keep track of the max
fdmax = newfd;
}
printf("selectserver: new connection from %s on "
"socket %d\n",
inet_ntop(remoteaddr.ss_family,
get_in_addr((struct sockaddr*)&remoteaddr),
remoteIP, INET6_ADDRSTRLEN),
newfd);
}
} else {
// handle data from a client
memset(buf, 0, 256);
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
// got error or connection closed by client
if (nbytes == 0) {
// connection closed
printf("selectserver: socket %d hung up\n", i);
} else {
perror("recv");
}
close(i); // bye!
FD_CLR(i, &master); // remove from master set
} else {
// we got some data from a client
for(j = 0; j <= fdmax; j++) {
// send to everyone!
if (FD_ISSET(j, &master)) {
// except the listener and ourselves
if (j != listener && j != i) {
if (send(j, buf, nbytes, 0) == -1) {
perror("send");
}
}
}
}
}
} // END handle data from client
} // END got new incoming connection
} // END looping through file descriptors
} // END for(;;)--and you thought it would never end!
return 0;
}
The reason a client can't receive a message until they send one is because.
fgets(message, 256,stdin);
Will keep "reading" (and will therefore block) until an EOF or a newline character has been read from the input stream
Also, note that
if( recv(sock , server_reply , 256 , 0) < 0)
blocks if there is nothing to read, which will prevent that user from sending more messages to the server until there is something new to read from the server. Assuming that you've played online games before, I hope that you can see that such a setup would be rather annoying!
So, we have to find someway of checking to see if we can read from STDIN and the server socket without incurring a block. Using select() will prevent us blocking on the sever socket, but it wouldn't work for STDIN whilst using fgets() to read input from the user. This is because, as mentioned above, fgets() blocks until an EOF or newline is detected.
The main solution I have in mind is to replace fgets with a method buffer_message() that will only read from STDIN when it won't block on read (we'll use select() to implement this). We'll then place what is read into a buffer. If there is a full message, this message will then be written to the server. Otherwise, we'll let the control keep going through the program until there is something to read or write.
This is code from a recent university assignment I did and so a small portion of the code isn't mine
Declarations:
//directives are above (e.g. #include ...)
//message buffer related delcartions/macros
int buffer_message(char * message);
int find_network_newline(char * message, int inbuf);
#define COMPLETE 0
#define BUF_SIZE 256
static int inbuf; // how many bytes are currently in the buffer?
static int room; // how much room left in buffer?
static char *after; // pointer to position after the received characters
//main starts below
Main:
//insert the code below into main, after you've connected to the server
puts("Connected\n");
//set up variables for select()
fd_set all_set, r_set;
int maxfd = sock + 1;
FD_ZERO(&all_set);
FD_SET(STDIN_FILENO, &all_set); FD_SET(sock, &all_set);
r_set = all_set;
struct timeval tv; tv.tv_sec = 2; tv.tv_usec = 0;
//set the initial position of after
after = message;
puts("Enter message: ");
//keep communicating with server
for(;;){
r_set = all_set;
//check to see if we can read from STDIN or sock
select(maxfd, &r_set, NULL, NULL, &tv);
if(FD_ISSET(STDIN_FILENO, &r_set)){
if(buffer_message(message) == COMPLETE){
//Send some data
if(send(sock, message, strlen(message) + 1, 0) < 0)//NOTE: we have to do strlen(message) + 1 because we MUST include '\0'
{
puts("Send failed");
return 1;
}
puts("Enter message:");
}
}
if(FD_ISSET(sock, &r_set)){
//Receive a reply from the server
if( recv(sock , server_reply , 256 , 0) < 0)
{
puts("recv failed");
break;
}
printf("\nServer Reply: %s\n", server_reply);
server_reply[0]='\0';
}
}
close(sock);
return 0;
//end of main
Buffer functions:
int buffer_message(char * message){
int bytes_read = read(STDIN_FILENO, after, 256 - inbuf);
short flag = -1; // indicates if returned_data has been set
inbuf += bytes_read;
int where; // location of network newline
// Step 1: call findeol, store result in where
where = find_network_newline(message, inbuf);
if (where >= 0) { // OK. we have a full line
// Step 2: place a null terminator at the end of the string
char * null_c = {'\0'};
memcpy(message + where, &null_c, 1);
// Step 3: update inbuf and remove the full line from the clients's buffer
memmove(message, message + where + 1, inbuf - (where + 1));
inbuf -= (where+1);
flag = 0;
}
// Step 4: update room and after, in preparation for the next read
room = sizeof(message) - inbuf;
after = message + inbuf;
return flag;
}
int find_network_newline(char * message, int bytes_inbuf){
int i;
for(i = 0; i<inbuf; i++){
if( *(message + i) == '\n')
return i;
}
return -1;
}
P.S.
if( send(sock , message , strlen(message) , 0) < 0)
The above can also block if there's no space to write to the server, but there's no need to worry about that here. Also, I'd like to point out a few things you should implement for your client and your server:
Whenever you send data over a network, the standard newline is \r\n, or carriage return / newline, or simply the network newline. All messages sent between the client and the server should have this appended at the end.
You should be buffering all data sent between the server and the client. Why? Because you're not guaranteed to receive all packets in a message in a single read of a socket. I don't have time to find a source, but when using TCP/IP, packets for a message/file don't have to arrive together, meaning that if you do read, you may not be reading all of the data you intend to read. I'm not well versed in this, so please investigate this more. Open to having this edited / corrected

sprintf / snprintf not correctly writing to buffer

I have to write a TCP server program that holds an integer, that should be modifiable by the client programs. It should kind of resemble a bankaccount. Everything works fine, except one thing:
When a client first connects to the server, it will wait for a welcoming message (the server has to be iterative, so it should only handle one client at a time). The server always just sends the first couple of letters of the welcoming message. All other messages are transferred completely and correctly.
In line 49, the welcoming message is first copied to a char-array and then written to the socket. This is where the error is... Only the first 1-5 letters are sent (different each time a new client connects). In other places where I use sprintf() to copy a message to a char-array and then writing it to the socket, everything works just like I want it to.
I have also tried using snprintf(), but that doesn't work either. What am I doing wrong? :D
So this would be an example output on client side:
Connected!
Waiting for welcome message...
We
After that, I could start entering commands to the server. But the whole welcome message is cut of after two letters. But as said above, sometimes its just one letter, sometimes its five :D.
Anyway, here's my code (if there are any other errors or things I should avoid, feel free to tell me :D):
client:
#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 <netdb.h>
#define BufferSize 99999
void error(const char *msg) {
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char msg[BufferSize], data[BufferSize];
if (argc < 3) error("usage: <hostname> <port>\n");
server = gethostbyname(argv[1]);
if (server == NULL) error("Host not found!");
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("socket() error");
bzero((char *) &serv_addr, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) error("connect() error");
printf("Connected!\nWaiting for welcome message...\n");
memset(msg, 0, BufferSize);
n = read(sockfd, msg, BufferSize - 1);
if (n < 0) error("read() error");
printf("%s\n", msg);
memset(data, 0, BufferSize);
while (fgets(data, BufferSize, stdin) != NULL) {
data[strlen(data) - 1] = '\0'; //remove trailing newline char
n = write(sockfd, data, strlen(data) + 1);
if (n < 0) error("write() error");
if (strcmp(data, "exit") == 0) break;
memset(msg, 0, BufferSize);
n = read(sockfd, msg, BufferSize - 1);
if (n < 0) error("read() error");
if (n==0) error("Server shut down...");
printf("%s\n", msg);
memset(data, 0, BufferSize);
}
close(sockfd);
return 0;
}
server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <limits.h>
#define BufferSize 99999
#define ClientWaiting 100
void error(const char *msg) {
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
int sockfd, newsockfd, portno, n, amount, balance, balOld;
socklen_t clilen;
char msg[BufferSize], data[BufferSize], *splitBuf[2];
struct sockaddr_in serv_addr, cli_addr;
balance = 0;
if (argc < 2) error("usage: <port>");
portno = atoi(argv[1]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("socket() error");
memset(&serv_addr, 0, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) error("bind() error");
listen(sockfd, ClientWaiting);
while (1) {
printf("Waiting for new client...\n");
clilen = sizeof (cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("accept() error");
printf("New connection accepted...\n");
memset(data, 0, BufferSize);
sprintf(data, "Welcome!\nPlease use the following commands:\n<put, get> <positive integer>\nBalance: %d€", balance);
n = write(newsockfd, data, strlen(msg) + 1);
if (n < 0) error("write() error");
while (1) {
splitBuf[0] = NULL;
splitBuf[1] = NULL;
memset(data, 0, BufferSize);
memset(msg, 0, BufferSize);
n = read(newsockfd, msg, BufferSize - 1);
if (n < 0) {
fprintf(stderr, "read() error\n");
break;
}
if (n == 0) {
printf("Client disconnected...\n");
break;
}
printf("Message received: %s\n", msg);
if (strcmp(msg, "exit") == 0) break;
splitBuf[0] = strtok(msg, " ");
splitBuf[1] = strtok(NULL, " ");
if (splitBuf[1] == NULL) {
strcpy(data, "Please use the following commands:\n<put, get> <positive integer>");
} else {
amount = atoi(splitBuf[1]);
if (amount <= 0) {
strcpy(data, "Please use the following commands:\n<put, get> <positive integer>");
} else if (strcmp(splitBuf[0], "put") == 0) {
balOld = balance;
balance += amount;
if (balance < balOld) {
balance = INT_MAX;
sprintf(data, "Warning! Overflow!\nBalance: %d€", balance);
} else {
sprintf(data, "Balance: %d€", balance);
}
printf("New balance: %d€\n", balance);
} else if (strcmp(splitBuf[0], "get") == 0) {
balOld = balance;
balance -= amount;
if (balance > balOld) {
balance = INT_MIN;
sprintf(data, "Warning! Underflow!\nBalance: %d€", balance);
} else {
sprintf(data, "Balance: %d€", balance);
}
printf("New balance: %d€\n", balance);
}
}
n = write(newsockfd, data, strlen(data) + 1);
if (n < 0) error("write() error");
}
close(newsockfd);
}
close(newsockfd);
close(sockfd);
return 0;
}
This example is way too long for people on SO to debug. (We're not compilers and debuggers.)
Your first step must be decomposing the program into smaller, more easily understood pieces that can be debugged independently. There are a couple of ways of doing that:
adding checkpoints that test your assertions
breaking code into more easily understandable and independently testable functions
For example, I look at:
splitBuf[0] = strtok(msg, " ");
splitBuf[1] = strtok(NULL, " ");
Does splitBuf contain what you expect? (Is NULL a valid parameter to strtok?)
I recommend two things:
#include
# Are my assumptions met?
assert( splitBuf[0]!=null );
assert( splitBuf[1]!=null );
#ifdef DEBUG
printf("splitBuf[0]=%s\n", splitBuf[0]);
printf("splitBuf[1]=%s\n", splitBuf[1]);
#endif
Compile with -DDEBUG=1 to ensure that DEBUG is defined, or add:
#define DEBUG
at the top of the file.
Second, programming is much easier if you tackle smaller problems, then work on and test your answers to those problems independently. Let's say you need to parse a message from the network and extract a balance, then you might write:
int parseBalance(char const* serverMessage) {
...
return balance;
}
You can now write a test:
void tests()
{
// test parseBalance
assert( 100 == parseBalance("100") )
... more tests
}
Minimally, you could call tests() at the start of your program to perform a self-test (read up on "unit testing" for better approaches).
IF you program in this way:
often the problems will become more easily apparent
if you are stuck and post to SO, you only need to post the minimal function and test case.
I believe your problem is in your first read() call. You do a single read, expecting to get all of the welcome message. But that isn't how TCP works. TCP puts the stream data into packets according to its own internal rules, and the receiving system can make it available in any amount it wants.
You cannot rely on getting all of the data the server wrote in a single read.
In fact the server is messed up too. You cannot expect a write call to write everything you say. The operating system's send buffer for the socket might be full or it might have had a signal interrupt the call.
You need send and receive buffers and functions to handle a loop around read and write that continues until you receive a complete line or send a complete buffer.

Sending multiple messages over a TCP port

I have a program that creates a socket (server and client program) and sends a message via a TCP port using that socket. My question is, how can I exchange multiple messages?
Every time I send a message the port gets closed and I need to use another port to send another message.
For example, I have to send 2 numbers from the client to the server and the server needs to reply back the total sum of the numbers I send. How would I achieve sending undefined number or even 2 numbers over the SAME port?
Here are the codes (pretty much standard stuff):
Server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
char* Itoa(int value, char* str, int radix)
{
static char dig[] =
"0123456789"
"abcdefghijklmnopqrstuvwxyz";
int n = 0, neg = 0;
unsigned int v;
char* p, *q;
char c;
if (radix == 10 && value < 0) {
value = -value;
neg = 1;
}
v = value;
do {
str[n++] = dig[v%radix];
v /= radix;
} while (v);
if (neg)
str[n++] = '-';
str[n] = '\0';
for (p = str, q = p + (n-1); p < q; ++p, --q)
c = *p, *p = *q, *q = c;
return str;
}
void error (const char *msg)
{
perror (msg);
exit (1);
}
int main (int argc, char *argv[])
{
if (argc < 2)
{
fprintf (stderr, "ERROR, no port provided\n");
exit (1);
}
//nova varijabla za sumiranje primljenih brojeva
int suma=0;
int sockfd, newsockfd, portno,i;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
for (i=0;i<2;i++)
{
sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error ("ERROR opening socket");
memset ((char *) &serv_addr, 0, sizeof (serv_addr));
portno = atoi (argv[1]);
portno+=i;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons (portno);
if (bind (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) error ("ERROR on binding");
//test za ispis otvorenog porta
printf("Uspjesno otvoren localhost na portu %d\n", portno);
listen (sockfd, 5);
clilen = sizeof (cli_addr);
newsockfd = accept (sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error ("ERROR on accept");
memset (buffer, 0, 256);
n = read (newsockfd, buffer, 255);
if (n < 0) error ("ERROR reading from socket");
printf ("%d. proslan broj: %s\n", i+1, buffer);
//print
suma=suma+atoi(buffer);
//radi!! printf("suma je %d\n", suma);
//od klijenta: n = write (sockfd, buffer, strlen (buffer));
//char * itoa ( int value, char * str, int base );
Itoa(suma, buffer, 10);
n = write (newsockfd, buffer, strlen(buffer));
if (n < 0) error ("ERROR writing to socket");
close (newsockfd);
close (sockfd);
}
return 0;
}
Client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error (const char *msg)
{
perror (msg);
exit (1);
}
int
main (int argc, char *argv[])
{
if (argc < 3)
{
fprintf (stderr, "usage %s hostname port\n", argv[0]);
exit (1);
}
int sockfd, portno, n,i;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
for (i=0;i<2;i++)
{
portno = atoi (argv[2]);
sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error ("Ne mogu otvoriti socket!");
server = gethostbyname (argv[1]);
if (server == NULL)
{
fprintf (stderr, "Greska, ne postoji!\n");
exit (1);
}
memset ((char *) &serv_addr, 0, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
bcopy ((char *) server->h_addr,
(char *) &serv_addr.sin_addr.s_addr,
server->h_length);
portno+=i;
serv_addr.sin_port = htons (portno);
if (connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0)
error ("ERROR connecting");
printf ("%d. broj za slanje: ", i+1);
memset (buffer, 0, 256);
fgets (buffer, 255, stdin);
n = write (sockfd, buffer, strlen (buffer));
if (n < 0)
error ("ERROR writing to socket");
memset (buffer, 0, 256);
n = read (sockfd, buffer, 255);
if (n < 0)
error ("ERROR reading from socket");
if (i==1) printf ("Suma iznosi: %s\n", buffer);
close (sockfd);
}
return 0;
}
So, for example, I run the code and get this for the server side:
j#PC ~/Desktop/Mreze/Lab1/rijeseno $ ./server2 5000
Uspjesno otvoren localhost na portu 5000
1. proslan broj: 45
Uspjesno otvoren localhost na portu 5001
2. proslan broj: 56
j#PC ~/Desktop/Mreze/Lab1/rijeseno $
And on the client side:
j#PC ~/Desktop/Mreze/Lab1/rijeseno $ ./client2 localhost 5000
1. broj za slanje: 45
2. broj za slanje: 56
Suma iznosi: 101
I've tried putting a while loop so it loops the part with sending but without success. Please explain to me where should I even put it so it works. Thank you!
Here is a problem:
n = read (newsockfd, buffer, 255);
What you do, is you perform read once, and the data might not be fully available. The fact is, you need to read data, as long as you received the data completely, or detected EOF condition (-1 return value).
Generally you need to write more reliable code for receiving part, as it is not guaranteed on stream protocol that your message boundaries are kept in any form.
Here is a (very unoptimal, yet simple) code for reading data:
int readLine(int fd, char data[])
{
size_t len = 0;
while (len < maxlen)
{
char c;
int ret = read(fd, &c, 1);
if (ret < 0)
{
data[len] = 0;
return len; // EOF reached
}
if (c == '\n')
{
data[len] = 0;
return len; // EOF reached
}
data[len++] = c;
}
}
And usage example:
char buffer[256];
int num1, num2;
readLine(newsockfd, buffer);
num1 = atoi(buffer);
readLine(newsockfd, buffer);
num2 = atoi(buffer);
First Put your connection() function before and close() after for loop. Just for an idea
connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)
for (i=0;i<n;i++){
// do you introspection with server
// actually send number to server
}
// Code to read result: SUM from server
close (sockfd);

Resources