Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Aug 8th, 2005, 5:50 PM   #11
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4 Brent is on a distinguished road
thanks
Brent is offline   Reply With Quote
Old Aug 8th, 2005, 8:04 PM   #12
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4 Brent is on a distinguished road
i have another problem....i can only send and recv 1 message. whats up with that. im thinking its probably because the char array is still filled with data, but im not sure. here is the code:

server.cpp
#include<winsock.h>
#include<windows.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
#pragma comment(lib,"wsock32.lib")
#define MAXBUFLEN 500


//functions
void send_recv();


//global variables
short int port;
int connected=false;
int sent=false;
int received=false;
int client_connected=false;
char rdata[MAXBUFLEN];
char data[500];


//socket stuff
SOCKET client;
SOCKET server;
WORD sockversion;
WSADATA wsaData;

int main(void)
{
	if((sockversion=WSAStartup(0x0101,&wsaData))!=SOCKET_ERROR)
	{	
		if((server=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))!=INVALID_SOCKET)
		{
			sockaddr_in from;
			int fromlen=sizeof(from);
			
			sockaddr_in sin;
			sin.sin_family=AF_INET;
			sin.sin_port=htons(80);
			sin.sin_addr.s_addr=INADDR_ANY;
			
			if((bind(server,(LPSOCKADDR)&sin,sizeof(sin)))!=SOCKET_ERROR)
			{	
				if((listen(server,4))!=SOCKET_ERROR)
				{
					if((client=accept(server,NULL,NULL))!=SOCKET_ERROR)
					{
						client_connected=true;
						connected=true;
						cout<<"\n";
						cout<<"-----------------------\n";
						cout<<"welcome to Network Chat\n";
						cout<<"-----------------------\n";
						cout<<"-hlp for help\n";
						cout<<"\n";
					
						while(true)
						{
							char escape[5]="-q";
							char ip_addr[5]="-ip";
							char clear[5]="-clr";
							char help[5]="-hlp";
							char disconn[5]="-dis";
							char conn[5]="-con";
							char status[6]="-stat";
						
							cout<<">> ";
							cin.get(data,sizeof(data));
							cin.ignore(80,'\n');		
							if(strcmp(data,escape)==0)
							{
								WSACleanup();
								closesocket(client);
								return 0;
							}
							else if(strcmp(data,ip_addr)==0)
							{
								cout<<"\n";
								cout<<"<your IP: "<<inet_ntoa(sin.sin_addr)<<"> "<<'\n';
								cout<<"<your port: "<<htons(sin.sin_port)<<">"<<'\n';
								cout<<"\n";
								cout<<"<server IP: "<<inet_ntoa(from.sin_addr)<<"> "<<'\n';
								cout<<"<server port: "<<htons(from.sin_port)<<"> "<<'\n';
								cout<<"\n";
							}
							else if(strcmp(data,clear)==0)
							{
								system("cls");
							}
							else if(strcmp(data,help)==0)
							{
								cout<<"\n";
								cout<<"-q    quit\n";
								cout<<"-clr  clear screen\n";
								cout<<"-ip   server IP and port #\n";
								cout<<"-dis  disconnect from server\n";
								cout<<"-stat connection status( 1 = connected, 0 = not connected)\n";
								cout<<"\n";
							}
							else if(strcmp(data,disconn)==0)
							{
								cout<<"\n";
								closesocket(server);
								closesocket(client);
								WSACleanup();
								connected=false;
								cout<<"you have disconnected\n";
								cout<<"\n";
							}
							else if(strcmp(data,status)==0)
							{
								cout<<"\n";
								cout<<"<connection status: "<<connected<<" >"<<'\n';
								cout<<"\n";
							}
							else
							{
								send_recv();
							}				
						}
					}
					else
					{
						connected=false;
						cout<<"failed accept\n";
						closesocket(server);
						closesocket(client);
						WSACleanup();
					}
				}
				else
				{
					connected=false;
					cout<<"failed bind\n";
					closesocket(server);
					closesocket(client);
					WSACleanup();
				}
			}
			else
			{
				connected=false;
				cout<<"failed bind\n";
				closesocket(server);
				closesocket(client);
				WSACleanup();
			}
		}
		else
		{
			connected=false;
			cout<<"invalid socket\n";
			WSACleanup();
			return 0;
		}
	}
	else
	{
		cout<<"WSAStartup() failed\n";
		WSACleanup();
		return 0;	
	}
}

void send_recv()
{	
	if((recv(client,rdata,MAXBUFLEN-1,0))==SOCKET_ERROR)
	{
		received=false;
		int error=WSAGetLastError();
		if(error==WSAENOTSOCK)
		{
			cout<<"not a valid socket\n";
			WSACleanup();
		}
		else if(error==WSAENOTCONN)
		{
			cout<<"socket not connected\n";
			WSACleanup();
		}
		cout<<"failed recv\n";
		WSACleanup();
	}
	else
	{
		received=true;
		cout<<"client says: "<<rdata<<'\n';
	}
	
	
	if((send(client,data,sizeof(data),0))==SOCKET_ERROR)
	{
		sent=false;
		cout<<"failed send\n";
		WSACleanup();
	}
	else
	{
		sent=true;
	}
	
	int addr_len;
	addr_len=sizeof(struct sockaddr*);
	
	sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(80);
	sin.sin_addr.s_addr=INADDR_ANY;

	closesocket(server);
	closesocket(client);
	connected=false;
	
	if((listen(server,4))!=SOCKET_ERROR)
	{
		sockaddr_in acc;
		acc.sin_family=AF_INET;
		acc.sin_port=htons(80);
		acc.sin_addr.s_addr=inet_addr("127.0.0.1");
			
		if((client=accept(server,NULL,NULL))!=SOCKET_ERROR)
		{
			connected=true;
		}
		else
		{
			cout<<"\n";
			cout<<"failed accept\n";
			cout<<"\n";
			connected=false;
		}
	}
	else
	{
		cout<<"\n";
		cout<<"failed listen\n";
		cout<<"\n";
		connected=false;
	}
}


client.cpp
#include<winsock.h>
#include<windows.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
#pragma comment(lib,"wsock32.lib")
#define MAXBUFLEN 500

void send_recv();

short int port;

char rdata[MAXBUFLEN];
char data[500];

SOCKET client;
SOCKET server;

int connected=false;
int sent=false;
int received=false;

int main(void)
{
	WORD sockversion;
	WSADATA wsaData;
	
	if((sockversion=WSAStartup(0x0101,&wsaData))!=SOCKET_ERROR)
	{	
		if((client=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))!=INVALID_SOCKET)
		{
			sockaddr_in from;
			int fromlen=sizeof(from);
			
			sockaddr_in sin;
			sin.sin_family=AF_INET;
			sin.sin_port=htons(80);
			sin.sin_addr.s_addr=inet_addr("127.0.0.1");
			
			if(connect(client,(LPSOCKADDR)&sin,sizeof(sin))!=SOCKET_ERROR)
			{	
				connected=true;
				cout<<"\n";
				cout<<"-----------------------\n";
				cout<<"welcome to Network Chat\n";
				cout<<"-----------------------\n";
				cout<<"-hlp for help\n";
				cout<<"\n";
			
				while(true)
				{
					char escape[5]="-q";
					char ip_addr[5]="-ip";
					char clear[5]="-clr";
					char help[5]="-hlp";
					char disconn[5]="-dis";
					char conn[5]="-con";
					char status[6]="-stat";
				
					cout<<">> ";
					cin.get(data,sizeof(data));
					cin.ignore(80,'\n');		
					if(strcmp(data,escape)==0)
					{
						WSACleanup();
						closesocket(client);
						return 0;
					}
					else if(strcmp(data,ip_addr)==0)
					{
						cout<<"\n";
						cout<<"<your IP: "<<inet_ntoa(sin.sin_addr)<<"> "<<'\n';
						cout<<"<your port: "<<htons(sin.sin_port)<<">"<<'\n';
						cout<<"\n";
						cout<<"<server IP: "<<inet_ntoa(from.sin_addr)<<"> "<<'\n';
						cout<<"<server port: "<<htons(from.sin_port)<<"> "<<'\n';
						cout<<"\n";
					}
					else if(strcmp(data,clear)==0)
					{
						system("cls");
					}
					else if(strcmp(data,help)==0)
					{
						cout<<"\n";
						cout<<"-q    quit\n";
						cout<<"-clr  clear screen\n";
						cout<<"-ip   server IP and port #\n";
						cout<<"-dis  disconnect from server\n";
						cout<<"-con  connect to server\n";
						cout<<"-stat connection status( 1 = connected, 0 = not connected)\n";
						cout<<"\n";
					}
					else if(strcmp(data,disconn)==0)
					{
						cout<<"\n";
						closesocket(server);
						closesocket(client);
						WSACleanup();
						connected=false;
						cout<<"you have disconnected\n";
						cout<<"\n";
					}
					else if(strcmp(data,conn)==0)
					{
						cout<<"\n";
						connect(client,(LPSOCKADDR)&sin,sizeof(sin));
						connected=true;
						cout<<"connected to server\n";
						cout<<"\n";
					}
					else if(strcmp(data,status)==0)
					{
						cout<<"\n";
						cout<<"<connection status: "<<connected<<" >"<<'\n';
						cout<<"\n";
					}
					else
					{
						send_recv();
					}				
				}
			}
			else
			{
				connected=false;
				cout<<"failed to connect to server\n";
				closesocket(client);
				WSACleanup();
			}
		}
		else
		{
			connected=false;
			cout<<"invalid socket\n";
			WSACleanup();
			return 0;
		}
	}
	else
	{
		cout<<"WSAStartup() failed\n";
		WSACleanup();
		return 0;	
	}
}

void send_recv()
{	
	if((send(client,data,sizeof(data),0))==SOCKET_ERROR)
	{
		sent=false;
		cout<<"failed send\n";
		WSACleanup();
	}
	else
	{
		sent=true;
	}
	
	int addr_len;
	addr_len=sizeof(struct sockaddr*); 
	
	if((recv(client,rdata,MAXBUFLEN-1,0))==SOCKET_ERROR)
	{
		received=false;
		int error=WSAGetLastError();
		if(error==WSAENOTSOCK)
		{
			cout<<"not a valid socket\n";
			WSACleanup();
		}
		else if(error==WSAENOTCONN)
		{
			cout<<"socket not connected\n";
			WSACleanup();
		}
		cout<<"failed recv\n";
		WSACleanup();
	}
	else
	{
		received=true;
		cout<<"server says: "<<rdata<<'\n';
	}
	
	sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(80);
	sin.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	closesocket(server);
	closesocket(client);
	WSACleanup();
	connected=false;
	if((connect(client,(LPSOCKADDR)&sin,sizeof(sin)))!=SOCKET_ERROR)
	{
		connected=true;
	}
}
Brent is offline   Reply With Quote
Old Aug 8th, 2005, 9:44 PM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Recommend you reduce your tab size. It's hellacious trying to keep a view of the code having to move up and down to look and then get at the horizontal scroll bar. Not having seen all the actual code, I'll just say that you don't "empty" the buffer, it just gets copied to the transmission medium, say, and then you write over it with new stuff. Data isn't going to refuse to write to it because there's something in it -- there's always something in it, if just uninitialized junk.
__________________
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 Aug 9th, 2005, 1:38 PM   #14
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4 Brent is on a distinguished road
sorry...ill try to keep the tags smaller. so what could be the problem with the program then?
Brent is offline   Reply With Quote
Old Aug 9th, 2005, 1:46 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Well, as I say, I couldn't assimilate it. I'll copy and paste it and run it thorough my C beautifier and take a look. It isn't the tags, it's the tabs. They're apparently 8 spaces, which makes for hellacious indentation, sometimes. I sometimes use 4 and sometimes use 3 because I'm wishy-washy and can't make up my mind.
__________________
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 Aug 9th, 2005, 2:34 PM   #16
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4 Brent is on a distinguished road
oh ok....thanks a bunch
Brent is offline   Reply With Quote
Old Aug 9th, 2005, 2:37 PM   #17
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4 Brent is on a distinguished road
In my text editor the tabs are only 4 spaces, but probably when I use those tags it mess's it all up.
Brent is offline   Reply With Quote
Old Aug 9th, 2005, 2:41 PM   #18
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It isn't a biggie, guy, I can deal with it. Some editors let you choose whether to leave the tabs or convert them to spaces. If you convert them to spaces, that reproduces 'bout anywhere. If you leave them tabs, it's at the discretion of the reproducing app (in this case, these pages), which might assign any number of spaces to them.
__________________
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 Aug 11th, 2005, 8:03 AM   #19
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
First, just to get the program ready to compile, I made the listed changes:
Quote:
// Use appropriate headers
// Changed to winsock2, more recent
// Added std namespace using directive
// Typo or copy error: SOCKET_ERRO R changed to SOCKET_ERROR
// Made all "main" paths have a return
For C++, the .h headers were provided for backward compatibility, are deprecated, and should not be used. I haven't any knowledge of "H-less" replacements for winsock or windows. My includes look like this:
#include<winsock2.h>
#include<windows.h>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<fstream>
//#pragma comment(lib,"wsock32.lib")

using namespace std;
In the following line, you are outputting from an uninitialized thangy, "from".
cout<<"<server IP: "<<inet_ntoa(from.sin_addr)<<"> "<<'\n';
I realize style is purely a personal thing, except when dictated by bosses/clients. Nevertheless, I'd ask if you write your emails and such without spaces between tokens. Clarity is an important plus and my personal opinion and experience is that the effort expended is not time wasted. Remember that ultimately a lot of our code is produced by habit. Best not to let the bad ones accumulate unduly.

I'll continue to look at the functionality of the code, thought I'd just drop the results of the initial whack on you.
__________________
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 Aug 11th, 2005, 1:20 PM   #20
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4 Brent is on a distinguished road
hey thanks...its helped alot. As for the style, thats the way I was taught, probably not the best habit.
Brent 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 5:00 PM.

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