Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 1st, 2005, 6:23 PM   #1
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4 Brent is on a distinguished road
problem with network application

whenever i make a network program it always says "failed recv()" or "failed send()", and i can never get it to work. any suggestions?


here's the server:
#include<winsock.h>
#include<iostream.h>
#include<string.h>
#pragma comment(lib,"wsock32.lib")

SOCKET server;
SOCKET client;

int main()
{
	WORD sockversion;
	WSADATA wsaData;
	int net;
	
	sockversion=MAKEWORD(1,1);
	WSAStartup(sockversion,&wsaData);
	
	server=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(server==INVALID_SOCKET)
	{
		cout<<"invalid socket\n";
		WSACleanup();
	}
	
	sockaddr_in sin;
	sin.sin_family=PF_INET;
	sin.sin_port=htons(8888);
	sin.sin_addr.s_addr=INADDR_ANY;
	
	net=bind(server,(LPSOCKADDR)&sin,sizeof(sin));
	if(net==SOCKET_ERROR)
	{
		cout<<"failed bind()";
		WSACleanup();
	}
	
	net=listen(server,4);
	if(net==SOCKET_ERROR)
	{
		cout<<"failed listen\n";
		WSACleanup();
	}
	
	client=accept(server,NULL,NULL);
	if(client==INVALID_SOCKET)
	{
		cout<<"failed accept\n";
		WSACleanup();
	}
	
	while(true);
	{
		char rdata[5000];
		char data[5000];
		
		net=recv(client,rdata,strlen(rdata),0);
		if(net==SOCKET_ERROR)
		{
			cout<<"failed recv()\n";
			WSACleanup();
		}
		
		cout<<"===================================\n";
		cout<<"\n";
		cout<<">>"<<rdata<<'\n';
		cout<<"<you said>"<<data<<'\n';
		cout<<"\n";
		cout<<"===================================\n";
		cout<<">> ";
		cin.get(data,4999);
		cin.ignore(80,'\n');
		
		net=send(server,data,strlen(data),0);
		if(net==SOCKET_ERROR)
		{
			cout<<"failed send\n";
			WSACleanup();
		}
	}
	
	WSACleanup();
	closesocket(server);
	closesocket(client);
	return 0;
}




here's the client

#include<winsock.h>
#include<iostream.h>
#include<string.h>
#pragma comment(lib,"wsock32.lib")

SOCKET server;
SOCKET client;

int main()
{
	WORD sockversion;
	WSADATA wsaData;
	int net;
	
	sockversion=MAKEWORD(1,1);
	WSAStartup(sockversion,&wsaData);
	
	client=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(client==INVALID_SOCKET)
	{
		cout<<"invalid socket\n";
		WSACleanup();
	}
	
	sockaddr_in sin;
	sin.sin_familt=PF_INET;
	sin.sin_port=htons(8888);
	sin.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	net=connect(client,(LPSOCKADDR)&sin,sizeof(sin));
	if(net==SOCKET_ERROR)
	{
		cout<<"failed to connect to server\n";
		WSACleanup();
	}
	
	
	while(true);
	{
		char rdata[5000];
		char data[5000];
		
		net=send(server,data,strlen(data),0);
		if(net==SOCKET_ERROR)
		{
			cout<<"failed send\n";
			WSACleanup();
		}
		
		cout<<"===================================\n";
		cout<<"\n";
		cout<<">>"<<rdata<<'\n';
		cout<<"<you said>"<<data<<'\n';
		cout<<"\n";
		cout<<"===================================\n";
		cout<<">> ";
		cin.get(data,4999);
		cin.ignore(80,'\n');
		
		net=recv(client,rdata,strlen(rdata),0);
		if(net==SOCKET_ERROR)
		{
			cout<<"failed recv()\n";
			WSACleanup();
		}
	}
	
	WSACleanup();
	closesocket(server);
	closesocket(client);
	return 0;
}


thanks in advance

Last edited by Mjordan2nd; Jul 1st, 2005 at 8:53 PM.
Brent is offline   Reply With Quote
Old Jul 1st, 2005, 7:25 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
These are HTML pages; they eat whitespace. Please put your code in tags to preserve its indentation. Otherwise, it's uglier than my ex-mother-in-law, and I won't read it. The "How to Post...." thread in the C forum is a good read, also.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 1st, 2005, 8:54 PM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Code tags added for ease on the eyes.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jul 2nd, 2005, 9:26 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
In this line in the server
		net=recv(client,rdata,strlen(rdata),0);
Don't use strlen, use sizeof - you want to be able to fill out your entire buffer, not just up to the first zero byte of the random data that is in there.

In this line in the client
		net=send(server,data,strlen(data),0);
You haven't put anything in data yet, so it contains random garbage, which might be a null in the first byte. This would mean you are attempting to send nothing, which probably would cause an error.

You need to either send the zero byte from the client to the server, to indicate end of string, or use the received length to add a zero byte to the end of the characters you are putting in rdata on the server, otherwise your string is unterminated.
The Dark is offline   Reply With Quote
Old Jul 12th, 2005, 6:10 PM   #5
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4 Brent is on a distinguished road
Quote:
You need to either send the zero byte from the client to the server, to indicate end of string, or use the received length to add a zero byte to the end of the characters you are putting in rdata on the server, otherwise your string is unterminated.
could you give me an example please...
Brent is offline   Reply With Quote
Old Jul 12th, 2005, 6:13 PM   #6
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4 Brent is on a distinguished road
Could someone tell me how to use the "select() function", or how to implement it in my program?
Brent is offline   Reply With Quote
Old Jul 12th, 2005, 7:02 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Go here in Beej's guide. I recommend you bookmark this or download a copy of the PDF version or something. Google is your friend if Beej falls through.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC