LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2007, 12:10 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
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.

Any idea what s wrong with
Code:
re=recv(accept_connection,command,strlen(command),0);
?
 
Old 02-12-2007, 03:13 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
i put a perror in there and got:
Code:
value of accept command =4
Hmm!: Software caused connection abort
The value of receive command = -1
GET /index.jsp HTTP/1.0
Code:
		cout << "value of accept command ="<<accept_connection<< endl;
		re=recv(accept_connection,command,strlen(command),0);
		perror("Hmm!");
 
Old 02-12-2007, 03:26 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
easy,
Code:
	char *command="GET /index.jsp HTTP/1.0\n\n";
is NOT a buffer. It's a pointer to somewhere.
make it a proper buffer
Code:
        #define SZ 512
        char command[SZ];
	strncat(command,"GET /index.jsp HTTP/1.0\r\n\r\n", SZ);
result:

Code:
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
 
Old 02-12-2007, 06:08 PM   #4
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
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;
 
Old 02-13-2007, 04:41 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
funny, cos as i posted I was getting everything.
maybe it's a client problem?

did you try it using my netcat example as the client.

you should learn to use netcat, it's indispensable for this work.
it can serve as an adhoc client or server, great for testing.
 
Old 02-14-2007, 03:09 AM   #6
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
I forgot about that part already, because it is less important from my new problem.

Now I suppose that client has sent the correct command, so my server is ready to send content of a text file to my client.

Code:
cout << line1 << endl;
cout << line1.c_str()<< endl;
bytes = send(accept_connection,line1.c_str(), sizeof(line1.c_str()),0);
cout <<line1.c_str();
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.
 
Old 02-14-2007, 03:40 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
what exactly are you doing?
why http?

do you know how http works? the construction of the header etc.?


try this:
Code:
printf  "HTTP/1.1\nContent-Length:6\nContent Type: text/html\n\nhELLO\n" | nc -lp 50125
and then point your browser at http://localhost:50125/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
recv command in sock in c mohtasham1983 Programming 2 02-07-2007 04:17 AM
pthreads and recv sayarsoft Linux - Kernel 2 08-11-2006 04:06 AM
Can't recv() Ephracis Programming 2 01-04-2005 02:49 PM
function recv() in socket husniteja Programming 1 08-18-2004 09:06 AM
recv() buffer linuxanswer Programming 1 03-22-2004 12:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration