Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old May 20th, 2006, 2:33 PM   #1
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
Socket Server

This is my first socket program. ive created a simple UDP server application which listens to a socket on port 1250 and gives an appropriate reply to based on the input recieved from client on the socket.
Please take a look at my code, comments and critique are appreciated. should i start building my client application now?
BTW. a question about the listen() function.
does the program excution halt on when you call listen() and continue when something is recieved.
or should the server listen(), then loop infinetly and check if something is recieved using accept(). and if accept() returns true then whatever routine should be executed?
#include<iostream>

#include <WinSock2.h>



using namespace std;

void WinConnect(); //open windows connection
int MakeSocket(); //create socket, return descriptor
void ServerSetup(sockaddr_in & , int);

int main()
{
	WinConnect();	
	int sd1 = MakeSocket();
	
	sockaddr_in server;					 // create server struct
	ServerSetup(server,sd1);
	

	listen(sd1,1);   //listen to created socket for connection
	accept(sd1,0,0); //accept connection on socket

	char* buffer = new char[25000];
	recv(sd1,buffer,25000,0);	//get recieved input and put in buffer
	char* reply = new char[25000];
	if (strcmp(buffer, "HELLO SERVER") == 0)
		{
			reply = "HI CLIENT";
			send(sd1,reply,strlen(reply),0);
		}
	else
		{
			reply = "UNKOWN COMMAND";
			send(sd1,reply,strlen(reply),0);
		}

	return 0;
}
void Winopen()
{
	WSADATA w;
	if (WSAStartup(0x0101, &w) != 0)
		{
			cout<< "ERROR: Could Not Open Windows connection."<<endl;
			exit(0);
		}

}
int MakeSocket()
{
int sd;
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
	{
		cout<< "ERROR: Could Not Create Socket."<<endl;
		WSACleanup();
		exit(0);
	}
return sd;
}

void ServerSetup(sockaddr_in & server , int sd)
{
	memset(&server, 0, sizeof(server));  //clears out our server struct

	server.sin_family = AF_INET;		//set server family.. *as socket*
	server.sin_port = htons(1250);				// set server's port number

	//assign server address to 255.255.255.255 
	//later make function to assign automatically using:
	//		gethostname(host_name, sizeof(host_name));
    //		hp = gethostbyname(host_name);
	unsigned char a1 = static_cast<unsigned char> (255);  
	server.sin_addr.S_un.S_un_b.s_b1 = a1;
	server.sin_addr.S_un.S_un_b.s_b2 = a1;
	server.sin_addr.S_un.S_un_b.s_b3 = a1;
	server.sin_addr.S_un.S_un_b.s_b4 = a1;

	//bind server adress to the socket created
	if(bind(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) ==SOCKET_ERROR)
	{
		WSACleanup();
		closesocket(sd);
		exit(0);
	}

}
hbe02 is offline   Reply With Quote
Old May 20th, 2006, 2:43 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
The program doesn't halt when listen() is called, it sets the socket specified so it is allowed to listen for new incomming connections. It's simple, if you don't enable mysock to listen(), mysock won't be able to accept() clients into the server.

By the way, I don't think the three function you've created are exactly necessary. They create more lines than are needed, especially the WinConnect() function. You shouldn't worry about making functions like these until you're working with slightly(a lot?) bigger programs.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old May 20th, 2006, 6:08 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by hbe02
void WinConnect(); //open windows connection

int main()
{
	WinConnect();

// many more lines

void Winopen()
{
	WSADATA w;
	if (WSAStartup(0x0101, &w) != 0)
		{
			cout<< "ERROR: Could Not Open Windows connection."<<endl;
			exit(0);
		}

}
Interesting, you have a prototype to WinConnect, and you even try calling it but you implement a Winopen function. A rename is likely to help there.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 20th, 2006, 6:53 PM   #4
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
hehe that was pretty stupid of me.. i was playing around with names and forgot to edit the function name.. thanks anyways.
So any other comments on my approach..?
hbe02 is offline   Reply With Quote
Old May 20th, 2006, 7:08 PM   #5
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
When i run this code.. bind returns false. and program is terminated.. why does binding to the socket fail?
hbe02 is offline   Reply With Quote
Old May 21st, 2006, 6:17 PM   #6
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Show us the changes in your code.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 22nd, 2006, 1:02 AM   #7
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
This is somewhat on-topic:
I have a program that returns SOCKET_ERROR on every recv and send. After a few hours of trying to figure out what was wrong I came to the conclusion nothing was wrong and I now just ignore the errors. My program works fine.

So although there is probably something wrong with your program, I have had some wierd experiences with winsock lately.
Harakim is offline   Reply With Quote
Old May 22nd, 2006, 1:15 AM   #8
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Quote:
Originally Posted by Harakim
So although there is probably something wrong with your program, I have had some wierd experiences with winsock lately.
It must be a conspiracy! All winsock libraries all of a sudden became corrupt. Oh wait, nevermind. You both just made a mistake, unfortunately we can't see them.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old May 22nd, 2006, 7:26 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
After a few hours of trying to figure out what was wrong I came to the conclusion nothing was wrong and I now just ignore the errors. My program works fine.
Please don't design any medical equipment, ATC programs, or ABS systems until I have ALREADY died. This message brought to you by one who is biting his tongue to avoid being really, really nasty.
__________________
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 May 22nd, 2006, 6:16 PM   #10
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
Harakim: i think your problem is with checking the error condition.. for example with send.. it returns the length of the string your sending..
for example if you send the string "Hello" which is of size 5.
then if (send == 5) no error occurs... else an error occured..
im not very sure about the recieving case.. but the recieve function should return one of 2 values.. returns 0 if error occured and -1 when there was no error.. so if (recieve == -1) no error.. else an error occured..

Alright i have a question on sockets.. lets say i want to listen on a socket.. or wait for data to be sent.. and at the same time run some code.. i want to print numbers 1 to infinity . i.e keep incrementing and print..and then when i recieve a message on the socket. the function halts and some other operation is executed..
hbe02 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 4:07 AM.

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