Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Aug 2nd, 2005, 12:02 PM   #1
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 249
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
Old Aug 2nd, 2005, 12:35 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
You'll probably have to use threads.
__________________
&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 Aug 2nd, 2005, 6:27 PM   #3
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4 Brent is on a distinguished road
what the heck is that?
Brent is offline   Reply With Quote
Old Aug 2nd, 2005, 7:07 PM   #4
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0 L7Sqr is an unknown quantity at this point
On windows:
google says
On *nix:
man pthread

And lokks like theres now:
pthreads for win32
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is online now   Reply With Quote
Old Aug 3rd, 2005, 3:54 PM   #5
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4 Brent is on a distinguished road
thanks
Brent is offline   Reply With Quote
Old Aug 3rd, 2005, 4:01 PM   #6
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4 Brent is on a distinguished road
how would i use the select() function in these programs
Brent is offline   Reply With Quote
Old Aug 3rd, 2005, 7:05 PM   #7
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
for Windows> check msdn.microsoft.com

for *nix...http://www.scit.wlv.ac.uk/~jphb/com...ets.html#select
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Aug 5th, 2005, 7:39 AM   #8
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4 Brent is on a distinguished road
is it best to use TCP or UDP for a chat program, and could someone recomend a website with a tutorial or something.
Brent is offline   Reply With Quote
Old Aug 7th, 2005, 8:41 PM   #9
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4 Brent is on a distinguished road
could someone show me how to do some simple multithreading
Brent is offline   Reply With Quote
Old Aug 8th, 2005, 7:52 AM   #10
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,466
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
check this out:

http://www.geocities.com/SiliconVall.../dthreads.html
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion 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 3:23 PM.

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