![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
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);
}
} |
|
|
|
|
|
#2 | |
|
Professional Programmer
|
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:
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! ▄▄▄▄ |
|
|
|
|
|
|
#3 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
![]()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
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..? |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
When i run this code.. bind returns false. and program is terminated.. why does binding to the socket fail?
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#8 | ||
|
Professional Programmer
|
Quote:
__________________
▄▄▄▄ Quote:
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! ▄▄▄▄ |
||
|
|
|
|
|
#9 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
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.. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|