LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 10-23-2009, 01:39 AM   #1
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 204

Rep: Reputation: 32
read / recv function in TCP server program (C Lang.) is not working.


i wrote a TCP server in C language. Code is as follows

Code:
using namespace std;

#include<arpa/inet.h>
#include<stdlib.h>
#include<fstream>
#include<iostream>

main()
{
	int sockfd;
	struct sockaddr_in cli_addr,server_addr;
	char buffer[100];
	ofstream myfile;
	socklen_t clilen;

	if( (sockfd = socket(PF_INET,SOCK_STREAM,0)) < 0 )
	{
		cout<<"server - unable to open the socket."<<endl;
		exit(1);
	}
	server_addr.sin_family            =       PF_INET;
	server_addr.sin_addr.s_addr       =       inet_addr("172.16.16.33");
	server_addr.sin_port              =       htons(2009);
	if( bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0 )
	{
		cout<<"Server - unable to bind the local address";
		exit(1);
	}
	listen(sockfd,1);
	while(1)
	{
		clilen = sizeof(cli_addr);
		bzero(buffer,100);
		if(accept(sockfd, (struct sockaddr*)&cli_addr, &clilen) < 0)
		{
			cout<<"Server - unable to accept the connection from client.";
                        exit(1);
		}
		if(recv(sockfd,buffer,100,0)<0)
		{
			cout<<"Server - unable to read the data from the socket.";
			exit(1);
		}
		myfile.open("data_file",ios::app);
		if(!myfile.is_open())
		{
			cout<<"Server - Unable to open file to write alert"<<endl;
			exit(1);
		}
		myfile<<buffer;
		myfile.close();
		}
	close(sockfd);
}
I tried the recv / read function for reading the data from the socket.
But it is not able to read the data from the socket. Always it is returning -1 and exit function is executing. I tried even as root user. Still same problem. Don't know where is the bug.

Client program is working correctly. it returning the correct number of bytes when I use write/send function.

can any one please help me sorting out this issue.

thanks.
 
Old 10-24-2009, 01:11 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Accept returns a new socket descriptor that you need to use for the recv and send functions.

See the simple-server example here

Further there is a dedicated programming section here at LQ.
 
Old 10-25-2009, 09:43 PM   #3
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 204

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by Wim Sturkenboom View Post
Accept returns a new socket descriptor that you need to use for the recv and send functions.
thanks. I corrected the code. It is working fine now.
thanks a lot.
 
  


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
multiple send() and recv() in one TCP connection ramyasugu Linux - Networking 0 03-17-2009 06:44 AM
problems in multithreaded send()/recv() using tcp socket arslanali Programming 2 05-26-2007 09:00 AM
recv() function of SOCKET ashucool83 Programming 1 10-08-2005 07:38 PM
function recv() in socket husniteja Programming 1 08-18-2004 09:06 AM
Red Hat 9 - How do I change TCP send/recv windows? oswald21 Linux - Networking 1 07-16-2003 05:21 PM


All times are GMT -5. The time now is 08:54 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration