ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408
Rep:
recv command in sock in c
i have a http client and server which must interact with each other using GET command. My client is working fine. But I am having problem with the server one.
Code:
#include <sys/socket.h>
#include <sys/types.h>
#include <iostream>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
using namespace std;
#include <arpa/inet.h>
int main()
{
register int bytes;
int sockfd,re,se,listen_connection,accept_connection,fromlen;
struct sockaddr_in myaddr,from;
int bufsize=1024; /* a 1K buffer */
char str[INET_ADDRSTRLEN];
char* buffer=(char*)malloc(bufsize);
FILE *fp;
char *command="GET /index.jsp HTTP/1.0\n\n";
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&myaddr, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(8070);
inet_ntop(AF_INET, &myaddr.sin_addr, str, sizeof(str));
se=bind(sockfd, (struct sockaddr *) &myaddr,sizeof(myaddr));
if (se!=0)
{
cout<<"Server cannot bind a port"<< endl;
exit(0);
}
listen_connection=listen(sockfd,5);
if (listen_connection!=0)
{
cout<<"Connection cannot be listened"<< endl;
exit(0);
}
// addrlen = sizeof(struct sockaddr_in);
for ( ; ; )
{
accept_connection=accept(sockfd,NULL,NULL);
if (accept_connection<0)
{
cout<<"Connection was refused"<< accept_connection <<endl;
exit(0);
}
cout << "value of accept command ="<<accept_connection<< endl;
re=recv(accept_connection,command,strlen(command),0);
cout <<"The value of receive command = "<< re<<endl;
cout <<command<<endl;
if (strcmp(command,"GET /index.jsp HTTP/1.0\n\n")==0)
{
cout << "You have chosen the right command"<< endl;
if ((fp=fopen("/home/mohi/temp/index.html","r")) == NULL)
{
cout << "Cannot open the file"<< endl;
exit(1);
}
while ((bytes = send(accept_connection, buffer, bufsize,0)) > 0)
{
write(1,buffer,bytes);
fputs(buffer, fp);
}
cout << bytes;
}
}
close(accept_connection);
exit(0);
}
I can receive command variable from client one but value of re is -1.
value of accept command =4
Hmm!: No error
The value of receive command = 15
this is a test
HTTP/1.0
FYI this is my client:
Code:
$ nc -vv localhost 8070
DNS fwd/rev mismatch: BTG142169.iuser.iroot.adidom.com != localhost
BTG142169.iuser.iroot.adidom.com [127.0.0.1] 8070 (?) open : Operation now in progress
this is a test
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408
Original Poster
Rep:
thanks for help.
I am having another problem now. Now recv function returns 1 which means it can receive message from client, but it receives only one character of the client message. I added MSG_WAITALL as the last parameter in recv function. Then I used strcpy to compy the message from the client, but it returned only the first letter. Then I tried to define a single counter and used a loop to receive the message from client and used strcat to store the whole message in a char variable. This worked for me but it returned some extra characters; however, I don't want to define a fixed variable for this loop, since different client may send messages in different lenghts.
Here is the code:
Code:
cout << "value of accept command ="<<accept_connection<< endl;
while (j<26)
{
re=recv(accept_connection,command,strlen(command),0);
j++;
strcat(command1,command);
cout <<re<<endl;
}
cout <<"The value of receive command = "<<command1<<endl;
As you see, first two lines output the same thing, but after send the value becomes different and client can receive a small part of string. For example if line1 is "Hello World", my client receives only Hell part of it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.