Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Aug 5th, 2005, 6: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
UDP network application

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

#define MAXBUFLEN 100 

void send_data();
void recv_data();

char data[500];
char rdata[MAXBUFLEN];
SOCKET server;
SOCKET client;

int main(void)
{
	WORD sockversion;
	WSADATA wsaData;
	
	int conn;

	sockversion=WSAStartup(0x101,&wsaData);
	
	if((client=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP))!=INVALID_SOCKET)
	{
		sockaddr_in server;
		server.sin_family=PF_INET;
		server.sin_port=htons(8888);
		server.sin_addr.s_addr=inet_addr("127.0.0.1");
		cout<<"connected to server\n";
		while(true)
		{
				cout<<">> ";
				cin.get(data,499);
				cin.ignore(80,'\n');
				send_data();
				recv_data();
				cout<<rdata<<endl;
		}
	}
	else
	{
		cout<<"invalid socket\n";
		WSACleanup();
		return 0;
	}
}

void send_data()
{
	int sent;

	sockaddr_in to;
	to.sin_family=PF_INET;
	to.sin_port=htons(8888);
	to.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	sent=sendto(client,data,sizeof(data),0,(struct sockaddr*)&to,sizeof(to));
}


void recv_data()
{
	int addr_len;
	addr_len=sizeof(struct sockaddr); 
	
	sockaddr_in fro;
	fro.sin_family=PF_INET;
	fro.sin_port=htons(8888);
	fro.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	recvfrom(client,rdata,MAXBUFLEN-1,0,(struct sockaddr*)&fro,&addr_len);
}

I have created a simple UDP program and was wondering if anyone could just look it over and give me some suggestions to make it work better.
Brent is offline   Reply With Quote
Old Aug 5th, 2005, 6:42 PM   #2
prolog
Programmer
 
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4 prolog is on a distinguished road
Hi,

just a note. Wouldn't it be nice to shutdown the application cracefully via userinput ?
I didn't look into detail in the rest of the code as i'm too tired right now to be any helpful. I'll look over it again tomorrow if you like.


so far ...
__________________
-= C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do succeed, you will blow away your whole leg. =- Bjarne Stroustrup
prolog is offline   Reply With Quote
Old Aug 5th, 2005, 6:58 PM   #3
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
how do you use "userinput"?
Brent is offline   Reply With Quote
Old Aug 5th, 2005, 7:02 PM   #4
prolog
Programmer
 
Join Date: Jul 2005
Location: Germany
Posts: 69
Rep Power: 4 prolog is on a distinguished road
was it the wrong word ? I meant that one should probably be able to type "quit" or "exit" or something an you applications shuts down in consequence of this input.
__________________
-= C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do succeed, you will blow away your whole leg. =- Bjarne Stroustrup
prolog is offline   Reply With Quote
Old Aug 5th, 2005, 8:00 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
how would i do this?
Brent is offline   Reply With Quote
Old Aug 5th, 2005, 8:10 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
here is what i came up with:

#include<winsock.h>
#include<windows.h>
#include<iostream.h>
#pragma comment(lib,"wsock32.lib")
#define MAXBUFLEN 100 

void send_data();
void recv_data();
void quit();

char data[500];
char rdata[MAXBUFLEN];
SOCKET server;
SOCKET client;

int main(void)
{
	WORD sockversion;
	WSADATA wsaData;
	
	int conn;

	sockversion=WSAStartup(0x101,&wsaData);
	
	if((client=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP))!=INVALID_SOCKET)
	{
		sockaddr_in server;
		server.sin_family=PF_INET;
		server.sin_port=htons(8888);
		server.sin_addr.s_addr=inet_addr("127.0.0.1");
		cout<<"connected to server\n";
		while(true)
		{
			char escape[5]="quit";
			cout<<">> ";
			cin.get(data,499);
			cin.ignore(80,'\n');
			if(strcmp(rdata,escape)==0)
			{
				quit();
			}
			send_data();
			recv_data();
			cout<<rdata<<endl;
		}
	}
	else
	{
		cout<<"invalid socket\n";
		WSACleanup();
		return 0;
	}
}

void send_data()
{
	int sent;

	sockaddr_in to;
	to.sin_family=PF_INET;
	to.sin_port=htons(8888);
	to.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	if((sendto(client,data,sizeof(data),0,(struct sockaddr*)&to,sizeof(to)))==SOCKET_ERROR)
	{
		cout<<"failed send\n";
		WSACleanup();
	}
}

void recv_data()
{
	int addr_len;
	addr_len=sizeof(struct sockaddr); 
	
	sockaddr_in fro;
	fro.sin_family=PF_INET;
	fro.sin_port=htons(8888);
	fro.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	if((recvfrom(client,rdata,MAXBUFLEN-1,0,(struct sockaddr*)&fro,&addr_len))==SOCKET_ERROR)
	{
		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();
	}
}

void quit()
{
	cout<<"goodbye\n";
	WSACleanup();
	closesocket(client);
	closesocket(server);
}
Brent is offline   Reply With Quote
Old Aug 6th, 2005, 5:59 PM   #7
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
the last post didnt work so I made a few changes:
#include<winsock.h>
#include<windows.h>
#include<iostream.h>
#include<stdlib.h>
#pragma comment(lib,"wsock32.lib")
#define MAXBUFLEN 100 

void send_data();
void recv_data();

char data[500];
char rdata[MAXBUFLEN];
SOCKET server;
SOCKET client;

int main(void)
{
	WORD sockversion;
	WSADATA wsaData;
	
	int conn;

	sockversion=WSAStartup(0x101,&wsaData);
	
	if((client=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP))!=INVALID_SOCKET)
	{
		sockaddr_in server;
		server.sin_family=PF_INET;
		server.sin_port=htons(19783);
		server.sin_addr.s_addr=inet_addr("127.0.0.1");
		cout<<"connected to server\n";
		while(true)
		{
			char escape[5]="-q";
			char ip_addr[5]="-ip";
			char clear[5]="clear";
			sockaddr_in from;
			int fromlen=sizeof(from);
		
			cout<<">> ";
			cin.get(data,499);
			cin.ignore(80,'\n');
			if(strcmp(data,escape)==0)
			{
				WSACleanup();
				closesocket(client);
				return 0;
			}
			else if(strcmp(data,ip_addr)==0)
			{
				cout<<"<server IP> "<<inet_ntoa(from.sin_addr)<<endl;
			}
			else
			{
				send_data();
				recv_data();
				cout<<rdata<<endl;
			}
		}
	}
	else
	{
		cout<<"invalid socket\n";
		WSACleanup();
		return 0;
	}
}

void send_data()
{
	int sent;

	sockaddr_in to;
	to.sin_family=PF_INET;
	to.sin_port=htons(19783);
	to.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	if((sendto(client,data,sizeof(data),0,(struct sockaddr*)&to,sizeof(to)))==SOCKET_ERROR)
	{
		cout<<"failed send\n";
		WSACleanup();
	}
}

void recv_data()
{
	int addr_len;
	addr_len=sizeof(struct sockaddr); 
	
	sockaddr_in fro;
	fro.sin_family=PF_INET;
	fro.sin_port=htons(19783);
	fro.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	if((recvfrom(client,rdata,MAXBUFLEN-1,0,(struct sockaddr*)&fro,&addr_len))==SOCKET_ERROR)
	{
		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();
	}
}

what I did was create a char array named escape[5]="-q", and if the user input was equal to escape then it would close the socket, and return 0.

now I have another question. Could someone show me how to use select in this program. I want to make it so the program can send and receive data at the same time to make it more efficient. I have read countless tutorials including Beej's tutorial, and I cant seem to figure it out. Im sure its just something stupid, but any help would be greatly appreciated.
Brent is offline   Reply With Quote
Old Aug 7th, 2005, 6:10 PM   #8
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
I had some free time last night so I revised it again:

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

void send_recv();

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

SOCKET client;
SOCKET server;

int main(void)
{
	WORD sockversion;
	WSADATA wsaData;
	int conn;

	if((sockversion=WSAStartup(0x101,&wsaData))!=SOCKET_ERROR)
	{	
		if((client=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP))!=INVALID_SOCKET)
		{
			sockaddr_in from;
			int fromlen=sizeof(from);
			
			sockaddr_in to;
			to.sin_family=AF_INET;
			to.sin_port=htons(8888);
			to.sin_addr.s_addr=inet_addr("127.0.0.1");
			
			if(connect(client,(LPSOCKADDR)&to,sizeof(to))!=SOCKET_ERROR)
			{	
				cout<<"\n";
				cout<<"connected to server\n";
				cout<<"\n";
				cout<<"------------------------------\n";
				cout<<"welcome to brents chat program\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";
				
					cout<<">> ";
					cin.get(data,499);
					cin.ignore(80,'\n');		
					if(strcmp(data,escape)==0)
					{
						WSACleanup();
						closesocket(client);
						return 0;
					}
					else if(strcmp(data,ip_addr)==0)
					{
						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  get server IP and port #\n";
						cout<<"-dis disconnect from server\n";
						cout<<"\n";
					}
					else if(strcmp(data,disconn)==0)
					{
						cout<<"\n";
						cout<<"you have disconnected\n";
						closesocket(server);
						closesocket(client);
						WSACleanup();
						cout<<"\n";
					}
					else
					{
						send_recv();
						cout<<rdata<<endl;
					}				
				}
			}
			else
			{
				cout<<"failed to connect to server\n";
				closesocket(client);
				WSACleanup();
			}
		}
		else
		{
			cout<<"invalid socket\n";
			WSACleanup();
			return 0;
		}
	}
	else
	{
		cout<<"WSAStartup() failed\n";
		WSACleanup();
		return 0;	
	}
}

void send_recv()
{
	int sent;
	
	sockaddr_in to;
	to.sin_family=AF_INET;
	to.sin_port=htons(8888);
	to.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	if((sendto(client,data,sizeof(data),0,(LPSOCKADDR)&to,sizeof(to)))==SOCKET_ERROR)
	{
		cout<<"failed send\n";
		WSACleanup();
	}
	
	int addr_len;
	addr_len=sizeof(struct sockaddr*); 
	
	sockaddr_in fro;
	fro.sin_family=AF_INET;
	fro.sin_port=htons(8888);
	fro.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	if((recvfrom(server,rdata,MAXBUFLEN-1,0,(LPSOCKADDR)&fro,&addr_len))==SOCKET_ERROR)
	{
		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
	{
		cout<<"server says: "<<rdata<<'\n';
	}
}

I made a few more changes:

1) added to the menu
2)combined the send_data() and recv_data() functions
and created the send_recv() function
3)I created the socket using PF_INET and filled sockaddr_in with AF_INET

if anyone has any suggestions that would improve this program please tell me, any help would be greatly appreciated.
Brent is offline   Reply With Quote
Old Aug 7th, 2005, 6:30 PM   #9
Shapeless
Programmer
 
Shapeless's Avatar
 
Join Date: Jul 2005
Location: germany/hamburg
Posts: 32
Rep Power: 0 Shapeless is on a distinguished road
Send a message via ICQ to Shapeless Send a message via MSN to Shapeless
hhm, this is not going to help you at all, but i would recommend to use standard headers.
so dont use
<iostream.h>
<stdlib.h>
<fstream.h>
but
<iostream>
<cstdlib>
<fstream>
as well as the std namespace.

stop useing
#define
and use
const type=blubb;
that means in your case
static const int max_buflen=500;
as it is written in Scott Meyers's : Effective C++ ( Item 1: Prefer const and inline to #define)
__________________
of all the things he has lost, i think he misses his mind the most

typedef typename pizza_t<oven_policy<225,12.5>,ingredient_policy<salami,mushroom,cheese> > Pizza;
Shapeless is offline   Reply With Quote
Old Aug 9th, 2005, 1:58 PM   #10
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
I also have another problem. when I send the message to the server, it doesnt send. I am using the same code as above.
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 7:16 AM.

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