View Single Post
Old Aug 2nd, 2005, 12:02 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 program

im created a simple chat program, but it doesnt send or receive messages very well, and you have to send a message before you can receive the one that was sent to you. does anyone have any suggestions?


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

#define MAX_CLIENTS 30

int net;
int client_count=0;

ofstream outfile;

char data[50];
char rdata[50];

SOCKET server;
SOCKET client;

int main()
{
	WORD sockversion;
	WSADATA wsaData;
	
	sockversion=MAKEWORD(1,1);
	WSAStartup(sockversion,&wsaData);
	
	if((server=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))!=INVALID_SOCKET)
	{
		sockaddr_in sin;
		sin.sin_family=PF_INET;
		sin.sin_port=htons(14567);
		sin.sin_addr.s_addr=INADDR_ANY;
		
		if((net=bind(server,(LPSOCKADDR)&sin,sizeof(sin)))!=SOCKET_ERROR)
		{
			if((net=listen(server,MAX_CLIENTS))!=SOCKET_ERROR)
			{
				client_count++;
				cout<<"client found\n";
				if(client_count<=MAX_CLIENTS)
				{
					if((client=accept(server,NULL,NULL))!=SOCKET_ERROR)
					{
						cout<<client_count<<'\n';
						do
						{
							cout<<"enter message: ";
							cin.get(data,49);
							cin.ignore(80,'\n');
							if((net=send(server,data,sizeof(data),NULL))!=SOCKET_ERROR || (net=recv(client,rdata,49,NULL))!=SOCKET_ERROR)
							{
								if(strlen(rdata)==NULL)
								{
									cout<<" ";
								}
								else
								{
									cout<<rdata<<'\n';
								}
							}
						}while(client_count!>MAX_CLIENTS);
					}
					else
					{
						cout<<"failed accept\n";
						WSACleanup();
						closesocket(server);
						closesocket(client);
					}
				}
				else
				{
					cout<<"server cant handle anymore clients";
					WSACleanup();
					closesocket(server);
					closesocket(client);
				}
			}
			else
			{
				client_count--;
				cout<<"failed listen\n";
				WSACleanup();
				closesocket(server);
				closesocket(client);
			}
		}
		else
		{
			cout<<"failed bind\n";
			WSACleanup();
			closesocket(server);
			closesocket(client);
		}
	}
	else
	{
		cout<<"invalid socket\n";
		WSACleanup();
		closesocket(server);
		closesocket(client);
	}
	return(client_count);
}



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

int net;
char data[50];
char rdata[50];

ofstream outfile;

SOCKET server;
SOCKET client;

int main()
{
	WORD sockversion;
	WSADATA wsaData;
	
	sockversion=MAKEWORD(1,1);
	WSAStartup(sockversion,&wsaData);
	
	if((client=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))!=INVALID_SOCKET)
	{
		sockaddr_in sin;
		sin.sin_family=PF_INET;
		sin.sin_port=htons(14567);
		sin.sin_addr.s_addr=inet_addr("127.0.0.1");
		
		sockaddr_in from;
		int fromlen=sizeof(from);
		
		if((net=connect(client,(LPSOCKADDR)&sin,sizeof(sin)))!=SOCKET_ERROR)
		{
			cout<<"<connected to "<<inet_ntoa(from.sin_addr)<<">"<<'\n';
			cout<<"<on port "<<"8888"<<">"<<'\n';
			do
			{	
				cout<<"enter a message ";
				cin.get(data,49);
				cin.ignore(80,'\n');
				cout<<rdata<<'\n';			
				if((net=send(client,data,sizeof(data),NULL))!=SOCKET_ERROR || (net=recv(server,rdata,49,NULL))!=SOCKET_ERROR)
				{
					if(strlen(rdata)==NULL)
					{
						cout<<" ";
					}
					else
					{
						cout<<rdata<<'\n';
					}
				}
			}while(!NULL);
		}
		else
		{
			cout<<"failed to connect to server\n";
			WSACleanup();
			closesocket(client);
			closesocket(server);
		}
	}
	else
	{
		cout<<"invalid socket\n";
		WSACleanup();
		closesocket(client);
		closesocket(server);
	}
}
Brent is offline   Reply With Quote