Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jul 1st, 2005, 6:23 PM   #1
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 252
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
 

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 10:03 PM.

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