Select server multi reading problems - c

I'm working on a server/client application in C
Actually I'm trying to allow the server to accept new clients and receiving data at (almost) the same time.
I send data two times,the first time I send a login and it works.
The second times I send some string data and it's like the client send them again and again but I've checked and they're sent only once.
Can someone please help me ?
I use gcc -Wall -pedantic to compile them.
Here is the client code : An argument is needed and it can be any text
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#define PATH "soquette"
#define BACKLOG 2
#define TAILLE_BUFFER 256
#define TIME_SLEEP 10
int main(int argc,char ** argv){
if(argc == 2){
struct sockaddr_un addr;
int serveur_socket;
ssize_t taille_lue;
char buffer[TAILLE_BUFFER];
char * buffer2;
if((serveur_socket = socket(PF_UNIX,SOCK_STREAM,0))<0){
perror("socket \n");
exit(EXIT_FAILURE);
}
memset(&addr,0,sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path,PATH,sizeof(addr.sun_path)-1);
if(connect(serveur_socket,(struct sockaddr *)&addr,sizeof(struct sockaddr_un))<0){
perror("connect \n");
exit(EXIT_FAILURE);
}
printf("pseudo %s \n",argv[1]);
if(write(serveur_socket,argv[1],strlen(argv[1])*sizeof(char))<0){
perror("1 st write \n");
exit(EXIT_FAILURE);
}
sleep(5);
taille_lue = read(STDIN_FILENO,buffer,TAILLE_BUFFER);
buffer2 = malloc(sizeof(int) + taille_lue * sizeof(char));
sprintf(buffer2,"%ld",taille_lue);
strcat(buffer2,buffer);
if(write(serveur_socket,buffer2,sizeof(buffer2))<0){
perror("write \n");
exit(EXIT_FAILURE);
}
printf("message envoyé %s \n",buffer2);
free(buffer2);
exit(EXIT_SUCCESS);
}
else{
printf("bad arguments number \n");
exit(EXIT_SUCCESS);
}
exit(EXIT_SUCCESS);
}
And here is the server side.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <signal.h>
#include <fcntl.h>
#define PATH "soquette"
#define NB_MAX_CONNECTION 10
#define TAILLE_BUFFER 256
#define NB_BOUCLE 10
#define TIME_WAIT 10
int socket_server;
void signal_handler(){
printf("signal handler \n");
if(close(socket_server)==-1){
perror("close \n");
}
if(unlink(PATH)==-1){
perror("unlink \n");
}
exit(EXIT_SUCCESS);
}
int main(){
int i,retval,j,fd_max,new_fd;
ssize_t taille_recue;
struct sockaddr_un addr;
char buffer[TAILLE_BUFFER];
struct timeval tv;
fd_set rfds,active_fd_set;
if(signal(SIGINT,signal_handler)==SIG_ERR){
perror("signal \n");
}
tv.tv_sec=TIME_WAIT;
tv.tv_usec=0;
FD_ZERO(&rfds);
FD_ZERO(&active_fd_set);
printf("server launch \n");
if((socket_server = socket(PF_UNIX,SOCK_STREAM,0))<0){
perror("socket \n");
exit(EXIT_FAILURE);
}
memset(&addr,0,sizeof(struct sockaddr_un));
addr.sun_family = PF_UNIX;
strncpy(addr.sun_path,PATH,sizeof(addr.sun_path)-1);
if((bind(socket_server,(struct sockaddr *)&addr,sizeof(struct sockaddr_un))==-1)){
perror("bind \n");
exit(EXIT_FAILURE);
}
if(listen(socket_server,NB_MAX_CONNECTION)==-1){
perror("listen \n");
exit(EXIT_FAILURE);
}
FD_SET(socket_server,&active_fd_set);
fd_max = socket_server;
for(i=0;i<NB_BOUCLE;i++){
FD_ZERO(&rfds);
rfds = active_fd_set;
printf("tour number %d \n",i);
if((retval = select(fd_max+1,&rfds,NULL,NULL,&tv))<0){
perror("select \n");
}
for(j=0;j<=fd_max;j++){
if(FD_ISSET(j,&rfds)){
if(j == socket_server){
if((new_fd = accept(socket_server,NULL,NULL))<0){
perror("accept \n");
signal_handler();
exit(EXIT_FAILURE);
}
printf("new client \n");
FD_SET(new_fd,&active_fd_set);
if(read(new_fd,buffer,TAILLE_BUFFER)<0){
perror("read 1\n");
}
else{
printf("read from buffer %s \n",buffer);
fd_max = new_fd;
}
}
else{
printf("client already in the list \n");
if((taille_recue = read(j,buffer,sizeof(int)))<0){
if(taille_recue == 0){
close(j);
FD_CLR(j,&rfds);
}
else{
signal_handler();
perror("read server 2 \n");
exit(EXIT_FAILURE);
}
}
else{
printf("read from buffer %s \n",buffer);
FD_CLR(j,&rfds);
}
}
}
}
}
printf("fermeture du serveur \n");
close(socket_server);
unlink(PATH);
exit(EXIT_SUCCESS);
}
Here is the client output
/client 1
pseudo 1
salut
message envoyé 6salut
/0
and here is the server output
MacBook-Pro-de-Kevin:tp10 kevin$ ./server
server launch
tour number 0
new client
read from buffer 1
tour number 1
client already in the list
read from buffer 6sal
tour number 2
client already in the list
read from buffer ut
/
tour number 3
client already in the list
read from buffer ut
/
tour number 4
client already in the list
read from buffer ut
/
tour number 5
client already in the list
read from buffer ut
/
tour number 6
client already in the list
read from buffer ut
/
tour number 7
client already in the list
read from buffer ut
/
tour number 8
client already in the list
read from buffer ut
/
tour number 9
client already in the list
read from buffer ut
/
fermeture du serveur

The server does not handle connected sockets correctly
In the first place, when it accepts a new connection, the server immediately tries to read data from the socket. There may be no data available at that point, so the read can block. Although that does not explain the problem you asked about, it conflicts with your objective.
The server assumes that the fd of any newly-accepted connection must be the maximum fd in the set. Although it is not impacting you yet, that assumption is not safe. File descriptors are freed up and made available for reuse when they are closed.
The server does not update fd_max when it closes a connection. However, although this may result in subsequent select() calls not conforming strictly to that function's specification, it probably does not cause any actual misbehavior.
The server and client do not handle I/O correctly
You appear to assume that the client's write() calls always write the full number of bytes specified to them, and that up to all bytes written will be read by the the server's next read(). These assumptions are not safe in general, though you do have a good chance of them being met for unix-domain sockets. In general, for both read() and write() you must consider the return value not only to spot errors / end-of-file, but also to ensure that all expected bytes are written / read. You must be prepared to loop in order to transfer all needed bytes.
In a select()-based scenario, the looping described in the previous point needs to be via the select() loop, else you likely introduce blocking. You may therefore need to do per-connection accounting of how many more bytes you expect to read / write at any given time. Indeed, unless your server does nothing but shuffle bytes from sources to sinks as fast as it can, it very likely will need to maintain some per-connection state.
it's odd that for an established connection, you attempt to read only the number of bytes in an int on any given read, when in fact more bytes than that may be available and the buffer can accommodate more. That's here:
if((taille_recue = read(j,buffer,sizeof(int)))<0){
Now consider the above quoted line carefully: only when read() returns a negative value is the if block executed. In particular, that block is not executed when read() returns 0 to indicate end-of-file, but it is in that block, not the else block, where you test for the end-of-file condition. This is what causes the behavior you asked about. An open file positioned at EOF is always ready to read, but you mishandle the EOF signal from the read(), treating it as if data had been read instead of recognizing it for what it is.
Additionally, if you want to print the buffer contents via printf() and a %s field descriptor then you must be certain either to insert a null character ('\0') into the buffer after the valid data, or to use a maximum field width that limits the output to the number of valid bytes in the buffer.

Related

Broken Pipe for C-Socket. How to only keep server running?

In a simple program where I'm trying to send command-line inputs from client to server, I keep getting a "Broken Pipe" for the server side. I send a string to the server and the server returns the string as lower-case to the client.
Server:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include<string.h>
#include <ctype.h>
#include <unistd.h>
int main()
{
char str[100];
int listen_fd, comm_fd;
struct sockaddr_in servaddr;
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
bzero( &servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
servaddr.sin_port = htons(37892);
bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen(listen_fd, 10);
comm_fd = accept(listen_fd, (struct sockaddr*) NULL, NULL);
while(1){
bzero( str, 100);
read(comm_fd,str,100);
for(int i = 0; i < strlen(str); i++){
str[i] = tolower(str[i]);
}
printf("Echoing back - %s",str);
write(comm_fd, str, strlen(str)+1);
}
}
Client
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include<string.h>
#include<ctype.h>
#include <unistd.h>
int main(int argc,char **argv)
{
int sockfd,n;
char sendline[100];
char recvline[100];
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof servaddr);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(37892);
inet_pton(AF_INET,"127.0.0.1",&(servaddr.sin_addr));
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
if(argc==1) printf("\nNo arguments");
if (1){
{
bzero( sendline, 100);
bzero( recvline, 100);
strcpy(sendline, argv[1]);
write(sockfd,sendline,strlen(sendline)+1);
read(sockfd,recvline,100);
printf("%s",recvline);
}
}
}
The problem I found was that when the client's side is done sending the string, the command line input does not work like fgets() where the loop will wait for another user input. If I change the if(1) in the client's side to a while(1), it will obviously run an infinite loop as no new inputs are being added.
The dilemma is, how would I be able to keep the server's side running to continuously return the string to the client while processing single requests from the command line on the client's side?
Your program has two problems:
1) read() works differently than you think:
Normally read() will read up to a certain number of bytes from some file or stream (e.g. socket).
Because read() does not distinguish between different types of bytes (e.g. letters, the end-of-line marker or even the NUL byte) read() will not work like fgets() (reading line-wise).
read() is also allowed to "split" the data: If you do a write(..."Hello\n"...) on the client the server may receive "Hel" the first time you call read() and the next time it receives "lo\n".
And of course read() can concatenate data: Call write(..."Hello\n"...) and write(..."World\n"...) on the client and one single read() call may receive "Hello\nWorld\n".
And of course both effects may appear at the same time and you have to call read() three times receiving "Hel", "lo\nWo" and "rld\n".
TTYs (= the console (keyboard) and serial ports) have a special feature (which may be switched off) that makes the read() call behave like fgets(). However only TTYs have such a feature!
In the case of sockets read() will always wait for at least one byte to be received and return the (positive) number of bytes received as long as the connection is alive. As soon as read() returns zero or a negative value the connection has been dropped.
You have to use a while loop that processes data until the connection has been dropped.
You'll have to check the data received by read() if it contains the NUL byte to detect the "end" of the data - if "your" data is terminated by a NUL byte.
2) As soon as the client drops the connection the handle returned by accept() is useless.
You should close that handle to save memory and file descriptors (there is a limit on how many file descriptors you can have open at one time).
Then you have to call accept() again to wait for the client to establish a new connection.
Your client sends one request and reads one response.
It then exits without closing the socket.
Your server runs in a loop reading requests and sending responses.
Your server ignores end of stream.
Little or none of this code is error-checked.

read a sequence of integers from client and send it to server

I am new to socket programming and i want to read a sequence of integers from the client program and send the array with these integers to the server program and do some calculations there. But how do i do that? the array that im sending with write must be char* ? maybe read a line from stdin and clean it up from other characters than numbers and send it to server and then take each number seperately? but how do i do that? here is my code..
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
int main() {
int sockfd, answer=1;
struct sockaddr_un serv_addr;
if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("ERROR opening socket");
exit(1);
}
serv_addr.sun_family = AF_UNIX;
strcpy(remote.sun_path, "askisi3");
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
perror("ERROR connecting");
exit(1);
}
do{
printf("Enter a sequence of integers.\n");
//code here...
printf("Type 0 for exit or any number to continue.\n");
scanf("%d",&answer)
}while(answer=0);
return 0;
}
We are not going to write the key code for you, but in answer to your specific questions:
the array that im sending with write must be char* ?
The first thing to understand is that from the perspective of the communication channel itself, there are no arrays and no pointers, only a stream of bytes.
The second thing to understand is that the value of a pointer itself is meaningful only to one process, so sending that is useless. You may, however, want to send some or all of the data to which a given pointer points. In fact, that's precisely what the write() function does -- it sends some number of the bytes to which the provided pointer points.
The third thing to understand is that the details of what you should send depend on some kind of agreement between the communicating parties about what will be sent and in what form it will be sent. This is called an application-layer "protocol" (not to be confused with a network protocol such as TCP). Since you are writing both client and server, you get to choose that protocol.
maybe read a line from stdin and clean it up from other characters than numbers and send it to server and then take each number seperately?
That would be a viable alternative.
but how do i do that?
That is too broad a question for this venue.

My C program is frozen upon accepting a connection when tested with CURL

I apologize if my code is extensively long, but I'm attempting to make a local server that handles multiple local clients. I even imported ideas from http://www.binarytides.com/multiple-socket-connections-fdset-select-linux/ to try to get it to work with no success.
I run it using 82 for a parameter, and see as expected:
Socket made and ready
Accepting 10 users
I then use CURL to connect to 127.0.0.1:82 and curl stalls. In my program I see as expected:
CLIENT CONNECTION MADE on socket# 0!
But the problem is data isn't being sent from the server to the client.
I tried forcing a break in CURL (via ctrl+c) to see if anything happened on the server and nothing did. I even tried using a web browser to connect and received similar results (a hang-up).
If I force a break on the server (via ctrl+c), then I got what I expect, a disconnection message (like "Empty Reply from server" from CURL).
What I expected to see in my browser is:
Error
This is a hack-ed-server
What could I be doing wrong here? I'm sort-of new to the select() calls so I'm not sure if I configured them correctly.
Here's the code:
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <signal.h>
#include <time.h>
extern errno;
long asock=-1,nsock=-1;
void end_app(int s){
struct sigaction si;
si.sa_handler=SIG_DFL;si.sa_flags=0;
sigaction(SIGCHLD,&si,NULL);
sigaction(SIGTSTP,&si,NULL);
sigaction(SIGTTOU,&si,NULL);
sigaction(SIGTTIN,&si,NULL);
sigaction(SIGSEGV,&si,NULL);
sigaction(SIGTERM,&si,NULL);
sigaction(SIGHUP,&si,NULL);
char v[5000];
sprintf(v,"Abrupt exit detected sig# %d. Closing sockets.\n",s);
write(1,v,strlen(v));
if (asock > -1){close(asock);}
if (nsock > -1){close(nsock);}
}
const long trapsig(){
struct sigaction s,si;
si.sa_handler=SIG_IGN;si.sa_flags=0;
s.sa_handler=end_app;s.sa_flags=0;
sigaction(SIGCHLD,&si,NULL);sigaction(SIGTSTP,&si,NULL);sigaction(SIGTTOU,&si,NULL);sigaction(SIGTTIN,&si,NULL);
if (sigaction(SIGSEGV,&s,NULL)==-1){printf("Cant trap signal!\n");return 1;}
if (sigaction(SIGTERM,&s,NULL)==-1){printf("Cant trap signal!\n");return 1;}
if (sigaction(SIGHUP,&s,NULL)==-1){printf("Cant trap signal!\n");return 1;}
}
//getreq params in: req=external buffer for data
// reqsz=size of external buffer. I set 10000
// nsock=valid socket pointer from accept()
//
//getreq params out: reqsz=actual size of data returned
//
void getreq(char* req,unsigned long *reqsz,long nsock){
//bufsize=how many bytes to read at once. High values like 5000 cause a stall.
//buffer=buffer of data from recv call
const unsigned long ibs=*reqsz,bufsize=5000;
char buffer[ibs],*rp=req;
//spacect=# of spaces in data read
//szct=iterator variable
//mysz=total length of returned data
//bufct=buffer counter to prevent segfault
//recvsz=data size returned from recv or
// forced -2 if buffer hits capacity
// or 2nd space in returned data is found
unsigned long spacect=0,szct=0,mysz=0,bufct=0;
long recvsz=1;char *p=buffer;
//
//Expected data: GET /whatever HTTP/x.x but we
// want /whatever
//
//loop until 2nd space is found or
//ibs bytes of data have been processed
while (recvsz > 0 && bufct < ibs){
recvsz=recv(nsock, p, bufsize, 0);
if (recvsz < 1){break;}
for (szct=1;szct<=recvsz;szct++){
if (*p==' '){spacect++;if (spacect > 2){spacect=2;recvsz=-2;break;}}
if (spacect==1 && *p != ' '){mysz++;if (mysz <= *reqsz){*rp++=*p;}}
p++;bufct++;if (bufct > ibs){recvsz=-2;break;}
}
}
// Process rest of data to try to avoid client errors
while (recvsz == -2){
recvsz=recv(nsock, buffer, bufsize, 0);
}
*reqsz=mysz;
}
int main(int argc,char* argv[]){
if (trapsig() < 0){return 1;}
//set maximum users to 10 and allocate space for each
long maxusers=10;long csock[11];memset(csock,0,11);
//do sanity checks and bind local socket
if (!argv[1]){printf("Port # required\n");return 1;}
if ((asock=socket(AF_INET, SOCK_STREAM, 0)) < 1){printf("Can't make socket! %s\n",strerror(errno));return 1;}
struct sockaddr_in a;
memset(&a,0,sizeof(a));
a.sin_family=AF_INET;
a.sin_addr.s_addr=inet_addr("127.0.0.1");
a.sin_port=htons(strtol(argv[1],NULL,10));
if (bind(asock,(struct sockaddr*)&a, sizeof(a))==-1){printf("Can't bind socket! %s\n",strerror(errno));return 1;}
if (listen(asock,10) < 0){printf("Can't listen! %s\n",strerror(errno));return 1;}
printf("Socket made and ready\nAccepting %d users\n",maxusers);
while(1){
usleep(10); //sleep incase processor is overloaded
fd_set SR;long SMAX=asock,n,canadd=0;
FD_ZERO(&SR);FD_SET(asock,&SR);
for (n=0;n<maxusers;n++){
if (csock[n] > 0){FD_SET(csock[n],&SR);}else{canadd=1;}
if (csock[n] > SMAX){SMAX=csock[n];}
}
long act=select(SMAX+1,&SR,0,0,0);
if (act != EINTR && act < 0){printf("Select error\n");}
if (canadd==1 && FD_ISSET(asock,&SR)){
//incoming connection detected
socklen_t alen=sizeof(a);
if (nsock=accept(asock, (struct sockaddr*)&a, &alen)< 0){printf("Can't accept! %s\n",strerror(errno));close(asock);return -1;}
for (n=0;n<maxusers;n++){if (csock[n]==0){csock[n]=nsock;break;}}
printf("CLIENT CONNECTION MADE on socket# %d!\n",n);
fcntl(nsock, F_SETFD, O_NONBLOCK);
//program reaches here when client first connects
}
for (n=0;n<maxusers;n++){
if (csock[n] > 0 && FD_ISSET(csock[n],&SR)){
//this section never seems to execute
unsigned long reqsz=10000;
char req[reqsz];
printf("Checking incoming data...\n",n);
getreq(req,&reqsz,csock[n]);
if (reqsz > 0){
printf("Received %d bytes\nData: %s\n",reqsz,req);
const char buf[10000]={"HTTP/1.0 200 OK\nConnection: close\nContent-type: text/html\n\n<html><head><title>hacked</title></head><body><H1>Error</h1><p>This is a hack-ed-server</p></body></html>\n\n"};
send(csock[n],buf,strlen(buf),0);
}else{
printf("Received no data\n");
}
printf("Closing.\n");
close(csock[n]);
csock[n]=0;
}
}
}
printf("Closing sockets\n");
close(asock);
return 0;
}
Pay attention to warnings. This
if(nsock=accept(asock, (struct sockaddr*)&a, &alen)< 0)
is not parsed as you seem to expect. In fact it assigns nsock a result of comparison of accept return value vs 0 (which is false aka 0). You want some extra parenthesis:
if ((nsock = accept(asock, (struct sockaddr*)&a, &alen)) < 0)
This
long csock[11];memset(csock,0,11);
does not initialize csock completely -- just the first 11 bytes. You want memset(csock, 0, sizeof(csock));

multiple requests with recv() call [duplicate]

I have an application that reads large files from a server and hangs frequently on a particular machine. It has worked successfully under RHEL5.2 for a long time. We have recently upgraded to RHEL6.1 and it now hangs regularly.
I have created a test app that reproduces the problem. It hangs approx 98 times out of 100.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/time.h>
int mFD = 0;
void open_socket()
{
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
if (getaddrinfo("localhost", "60000", &hints, &res) != 0)
{
fprintf(stderr, "Exit %d\n", __LINE__);
exit(1);
}
mFD = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (mFD == -1)
{
fprintf(stderr, "Exit %d\n", __LINE__);
exit(1);
}
if (connect(mFD, res->ai_addr, res->ai_addrlen) < 0)
{
fprintf(stderr, "Exit %d\n", __LINE__);
exit(1);
}
freeaddrinfo(res);
}
void read_message(int size, void* data)
{
int bytesLeft = size;
int numRd = 0;
while (bytesLeft != 0)
{
fprintf(stderr, "reading %d bytes\n", bytesLeft);
/* Replacing MSG_WAITALL with 0 works fine */
int num = recv(mFD, data, bytesLeft, MSG_WAITALL);
if (num == 0)
{
break;
}
else if (num < 0 && errno != EINTR)
{
fprintf(stderr, "Exit %d\n", __LINE__);
exit(1);
}
else if (num > 0)
{
numRd += num;
data += num;
bytesLeft -= num;
fprintf(stderr, "read %d bytes - remaining = %d\n", num, bytesLeft);
}
}
fprintf(stderr, "read total of %d bytes\n", numRd);
}
int main(int argc, char **argv)
{
open_socket();
uint32_t raw_len = atoi(argv[1]);
char raw[raw_len];
read_message(raw_len, raw);
return 0;
}
Some notes from my testing:
If "localhost" maps to the loopback address 127.0.0.1, the app hangs on the call to recv() and NEVER returns.
If "localhost" maps to the ip of the machine, thus routing the packets via the ethernet interface, the app completes successfully.
When I experience a hang, the server sends a "TCP Window Full" message, and the client responds with a "TCP ZeroWindow" message (see image and attached tcpdump capture). From this point, it hangs forever with the server sending keep-alives and the client sending ZeroWindow messages. The client never seems to expand its window, allowing the transfer to complete.
During the hang, if I examine the output of "netstat -a", there is data in the servers send queue but the clients receive queue is empty.
If I remove the MSG_WAITALL flag from the recv() call, the app completes successfully.
The hanging issue only arises using the loopback interface on 1 particular machine. I suspect this may all be related to timing dependencies.
As I drop the size of the 'file', the likelihood of the hang occurring is reduced
The source for the test app can be found here:
Socket test source
The tcpdump capture from the loopback interface can be found here:
tcpdump capture
I reproduce the issue by issuing the following commands:
> gcc socket_test.c -o socket_test
> perl -e 'for (1..6000000){ print "a" }' | nc -l 60000
> ./socket_test 6000000
This sees 6000000 bytes sent to the test app which tries to read the data using a single call to recv().
I would love to hear any suggestions on what I might be doing wrong or any further ways to debug the issue.
MSG_WAITALL should block until all data has been received. From the manual page on recv:
This flag requests that the operation block until the full request is satisfied.
However, the buffers in the network stack probably are not large enough to contain everything, which is the reason for the error messages on the server. The client network stack simply can't hold that much data.
The solution is either to increase the buffer sizes (SO_RCVBUF option to setsockopt), split the message into smaller pieces, or receiving smaller chunks putting it into your own buffer. The last is what I would recommend.
Edit: I see in your code that you already do what I suggested (read smaller chunks with own buffering,) so just remove the MSG_WAITALL flag and it should work.
Oh, and when recv returns zero, that means the other end have closed the connection, and that you should do it too.
Consider these two possible rules:
The receiver may wait for the sender to send more before receiving what has already been sent.
The sender may wait for the receiver to receive what has already been sent before sending more.
We can have either of these rules, but we cannot have both of these rules.
Why? Because if the receiver is permitted to wait for the sender, that means the sender cannot wait for the receiver to receive before sending more, otherwise we deadlock. And if the sender is permitted to wait for the receiver, that means the receiver cannot wait for the sender to send before receiving more, otherwise we deadlock.
If both of these things happen at the same time, we deadlock. The sender will not send more until the receiver receives what has already been sent, and the receiver will not receive what has already been sent unless the sender send more. Boom.
TCP chooses rule 2 (for reasons that should be obvious). Thus it cannot support rule 1. But in your code, you are the receiver, and you are waiting for the sender to send more before you receive what has already been sent. So this will deadlock.

How to create a simple Proxy to access web servers in C

I’m trying to create an small Web Proxy in C. First, I’m trying to get a webpage, sending a GET frame to the server.
I don’t know what I have missed, but I am not receiving any response. I would really appreciate if you can help me to find what is missing in this code.
int main (int argc, char** argv) {
int cache_size, //size of the cache in KiB
port,
port_google = 80,
dir,
mySocket,
socket_google;
char google[] = "www.google.es", ip[16];
struct sockaddr_in socketAddr;
char buffer[10000000];
if (GetParameters(argc,argv,&cache_size,&port) != 0)
return -1;
GetIP (google, ip);
printf("ip2 = %s\n",ip);
dir = inet_addr (ip);
printf("ip3 = %i\n",dir);
/* Creation of a socket with Google */
socket_google = conectClient (port_google, dir, &socketAddr);
if (socket_google < 0) return -1;
else printf("Socket created\n");
sprintf(buffer,"GET /index.html HTTP/1.1\r\n\r\n");
if (write(socket_google, (void*)buffer, MESSAGE_LENGTH+1) < 0 )
return 1;
else printf("GET frame sent\n");
strcpy(buffer,"\n");
read(socket_google, buffer, sizeof(buffer));
// strcpy(message,buffer);
printf("%s\n", buffer);
return 0;
}
And this is the code I use to create the socket. I think this part is OK, but I copy it just in case.
int conectClient (int puerto, int direccion, struct sockaddr_in *socketAddr) {
int mySocket;
char error[1000];
if ( (mySocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("Error when creating the socket\n");
return -2;
}
socketAddr->sin_family = AF_INET;
socketAddr->sin_addr.s_addr = direccion;
socketAddr->sin_port = htons(puerto);
if (connect (mySocket, (struct sockaddr *)socketAddr,sizeof (*socketAddr)) == -1) {
snprintf(error, sizeof(error), "Error in %s:%d\n", __FILE__, __LINE__);
perror(error);
printf("%s\n",error);
printf ("-- Error when stablishing a connection\n");
return -1;
}
return mySocket;
}
Thanks!
First, you're not checking how many bytes the write(2) call actually wrote to the socket. The return value of the call tells you that. Same for the read(2). TCP socket is a bi-directional stream, so as a rule always do both in a loop until expected number of bytes is transferred, EOF is read (zero return from read(2)), or an error occurred (which you are not checking for when reading either).
Then HTTP is rather complex protocol. Make yourself familiar with RFC 2616, especially application level connection management and transfer encodings.
Edit 0:
Hmm, there's no such thing as "simple" proxy. You need to manage multiple connections (at least client-to-proxy and proxy-to-server), so it's probably best to look into select(2)/poll(2)/epoll(4)/kqueue(2) family of system call, which allow you to multiplex I/O. This is usually combined with non-blocking sockets. Look into helper libraries like libevent. Look at how this is done in good web-servers/proxies like nginx. Sound like it's a lot for you to discover, but don't worry, it's fun :)
Since you didn't post the GetIP routine, I am not certain that your hostname lookup is correct, as from the looks of it, I am not sure that you are using inet_addr function correctly.
Nikolai has pointed out some very good points (and I fully agree). In fact you GET request is actually broken, and while I was testing it on my own local Apache web server on my system, it didn't work.
sprintf(buffer,"GET /index.html HTTP/1.1\r\n\r\n");
if (write(socket_google, (void*)buffer, LONGITUD_MSJ+1) < 0 )
return 1;
else printf("GET frame sent\n");
...
strcpy(buffer,"\n");
read(socket_google, buffer, sizeof(buffer));
should be replaced with
snprintf(buffer, sizeof(buffer),
"GET / HTTP/1.1\r\nHost: %s\r\nUser-Agent: TEST 0.1\r\n\r\n",
google);
if (write(socket_google, buffer, strlen(buffer)+1) < 0 ) {
close(socket_google);
return 1;
} else
printf("GET frame sent\n");
...
buffer[0] = '\0';
/* Read message from socket */
bytes_recv = read(socket_google, buffer, sizeof(buffer));
if (bytes_recv < 0) {
fprintf(stderr, "socket read error: %s\n", strerror(errno));
close(socket_google);
exit(10);
}
buffer[bytes_recv] = '\0'; /* NUL character */
/* strcpy(message,buffer); */
printf("%s\n", buffer);
...
You should also close the socket before exiting the program. Enable standard C89/90 or C99 mode of your compiler (e.g. -std=c99 for gcc) and enable warnings (e.g. -Wall for gcc), and read them. And #include the necessary header files (assuming Linux in my case) for function prototypes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h> /* for gethostbyname() */
There is some casting of pointers and structs in regards to the hostname / IP address resolving, which can be confusing and easy place to make a mistake, so verify that is working as you expect it is.
in_addr_t ip;
...
GetIP(google, &ip); /* I changed the parameters */
printf("IP address = %x (%s)\n",
ip,
inet_ntoa(*((struct in_addr*)&ip)));
Actually, I've been implementing a small web proxy using my library called rzsocket link to it.
One of the most difficult things I've found when implementing the web proxy, perhaps this might also be your problem, was that, in order to make the proxy work properly, I had to set keep-alive settings false. One way of doing this in FireFox, is accessing about:config address, and setting the value of network.http.proxy.keep-alive to false.

Resources