View Full Version : Sockets....
G'n'R
09-15-2003, 08:07 AM
Hi All,
I am having trouble with Beej's Guide to Network programming. I understand the tutorial but...
I have (tried) written a client and server app. That is. 1 exe for
client and one for server.
They both compile and run, however the clien is not able to connect to the server. A few questions.
I am running them both locally on my computer. Can one do that?
To get the address of the computer, its under propeties for network...?
Bit confused about where to get the address required, since i think thats the one that gotta be wrong here. Anyway...
I have attached the zip with both files.
could anyone give me a hit to what is wrong and what to do about it..
Cheers,
G'n'R
Hammer
09-15-2003, 08:58 AM
>>I am running them both locally on my computer. Can one do that?
Yes.
>>To get the address of the computer
Just use 127.0.0.1 (that is localhost)
G'n'R
09-15-2003, 10:09 AM
Yep, cool. However, the server side wont bind to it. Keep getting error: Unalble to bind to port??
Here is the code. I am real new to sockets (so i have probably left out a few bits here)....but this compiles and runs. Except for the error on bind. If i use any other number for the ip it runs...
#include <winsock.h>
#include <stdio.h>
#include <conio.h>
#define SERVERPORT 4545
#define BACKLOG 20
struct sockaddr_in sAddress;
int clientHandles[BACKLOG];
struct sockaddr_in cAddress[BACKLOG];
int connectionCount=0;
struct hostent *he;
int socketHandle;
WSADATA wsaData; // use to inititalize winsock
int main(void){
if(!WSAStartup(MAKEWORD(1,1), &wsaData)==0)
return (0);
he = gethostbyname("lucifer");
if(!(socketHandle= socket(AF_INET, SOCK_STREAM, 0))){
printf("\nERR: Unable to create socket...");
while(!kbhit());
return(0);
}
sAddress.sin_family=AF_INET;
sAddress.sin_port = htons(SERVERPORT);
sAddress.sin_addr.S_un.S_addr= inet_addr ("127.0.0.1");//16177343;
memset(&(sAddress.sin_zero), '\0', 8);
// THIS IS WHERE IT STOPS WITH IP 127.0.0.1 ??
if(!bind(socketHandle, (struct sockaddr *)&sAddress, sizeof(struct sockaddr))){
int errorCode=WSAGetLastError();
printf("\nERR %i: Unable to bind to address...", errorCode);
while(!kbhit());
return (0);
}
// set the backlog for how many clients the server accepts
if(!listen(socketHandle, BACKLOG)){
printf("\nERR: Unable to listen to socket...");
while(!kbhit());
return (0);
}
char hostname[50];
gethostname(hostname, sizeof(hostname));
printf("Server Information:\n Family: %i --> Port: %i --> Address: %s ServerName: %s\n\n Waiting for connections...\n\n", sAddress.sin_family,
sAddress.sin_port,
inet_ntoa(sAddress.sin_addr),
hostname);
while(1){
int sin_size = sizeof(struct sockaddr_in);
if((clientHandles[connectionCount] =
accept(socketHandle, (struct sockaddr *)&cAddress[connectionCount], &sin_size))!=-1){
printf("\n\nAccepted Connection: \n\n ClintId: %i\n", clientHandles[connectionCount]);
send(clientHandles[0], "Welcome to RIO Server!", sizeof("Welcome to RIO Server"), 0);
connectionCount++;
}
if(kbhit())
break;
}
closesocket (socketHandle);
WSACleanup();
return(0);
}
G'n'R
09-15-2003, 01:51 PM
Yo,
Dont worry about it :)
After actually concentrating and looking threw the code without fear i managed it. Got rid of heaps of wrong if testing and typos. Its working :)
But who can give me a hint on how to create non bloking servers. e.g. so the accept command dosnt loop internally?(its not in the tutorial)
Cheers,
G'n'R
Hammer
09-15-2003, 04:21 PM
...
if (bind(socketHandle, (struct sockaddr *) &sAddress, sizeof(struct sockaddr)) == SOCKET_ERROR)
...
if (listen(socketHandle, BACKLOG) == SOCKET_ERROR)
...
G'n'R
09-16-2003, 10:11 AM
Hi all,
This socket thing is going great. I solved the blocking thing by using threads. My question is if the way i am doing it is the best way. What i am doing is:
I create a thread a beginning of program to poll on the accept function. Check to see if a new client is trying to connect
If one is I spawn a new thread (from the accept thread) for this client and inc client count. Clients and clientThreads are arrays.
The client thread checks to see if the client disconnects etc, using recv function.
----------------------------------------------------------------------------------
My questions is:
Calling threads from threads. Is this common practice and is it safe?
How many threads can one have? I have a max thread count at 25 at the moment.
Can you terminate a thread from within the source thread it self? e.g When the client exits, my server program catches that and exist the client thread loop. Can i use closehandle after i break out of the thread loop and before the thread function pointer exist? Right now close all threads before server exit. I just loop and close on the thread counter.
I need to find a better way using the client threads as since i am closing all threads at server exit the thread counter (for clients) at 25 wont last long.
Anyway....anyone got any suggestions?
G'n'R
Hammer
09-16-2003, 02:16 PM
Sounds like you have lots of questions. Maybe a tutorial or book (http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=utf-8&q=windows+thread+programming+c%2B%2B&btnG=Google+Search) would help you.
In essence, what you're doing sounds about right.
G'n'R
09-17-2003, 04:58 AM
Yep, and i got a lot more :)
But what i have done so far seems to work pretty good. But could you answer this.
Does this work over the internet. e.g. if i connect to the internet
with my modem. I then get the internet ip address (check my dial up connection, client id) and use that in my server app. I then get someone on the net to run my client program (connecting with the ip i am using). Will it work?
I guess what i am trying to find out is:
When binding a socket to an ip, what does the function do? Check threw a current internet connection, local networkcards etc.
If so, is there a function to check for a internet connection and if so get the internet address you are connected with?
Cheers,
G'n'R
Hammer
09-17-2003, 05:31 AM
>>Will it work?
Yes, providing you code it right.
>>check for a internet connection
OS/compiler specific, check your local documentation
>>if so get the internet address
See the FAQ for examples, or again your local docs.
G'n'R
09-17-2003, 06:58 AM
Hi Hammer,
Thanks for you replies. Could you be a little more specific about
your first answer. >>yes, provide....
Is there anything specific i need to do or take into account when trying to implement that?
Cheers,
G'n'R
Hammer
09-17-2003, 01:54 PM
Well, all I really meant was for you to code it and test it locally, then try to use a net based connection. As long as you're bound on an interface that is connected to the net, you should do OK. Check out beej's for INADDR_ANY.
G'n'R
09-18-2003, 12:31 AM
Yep, will do. It all works locally. And i have also tried locally using the internet address i get when i connect to the internet. That work to. So i guess the next test is to send the client to a friend and test it remotly......:) That will be pretty cool if it works....
Cheers,
G'n'R
Hammer
09-18-2003, 05:57 AM
Just make sure you're code is secure. There are plenty of buffer overflow bugs that leave servers exposed to attack. I'm sure you'll be safe, and no-one will target you, but it's good practice to write safe code.
G'n'R
09-18-2003, 10:17 AM
hmmmm, that dosnt sound cool at all :) Buffer overflows? I am currently reading in the tutorial about writing read/write functions that ensures all data is sent and recieved. e.g. loop on send/recieve until you get all the bytes you should get (as descriped in a packet struct). Is that what you mean? (I am not familiar with the terms yet). I will eventually encrypt the data being sent but that far off yet...Could you give a specific example on buffer overflow and online targeting etc...(in general terms etc)
G'n'R
Brian
09-18-2003, 10:40 AM
If you have an array of 25 chars:
char blah[25];
and someone can put data in that array:
scanf("%s",blah);
they could put more than 25 characters in it. (Unless you use a safer method of input, such as fgets). When an array is overflowed, they can overwrite the code of the program with their own machine language instructions*, causing the program to do whatever they want.
If it is possible to overflow an array remotely, it can be disasterous. The blaster worm is an example of this.
* The only thing overwritten normally is the return address for a function, and when the program returns, it returns to the beginning code you have entered into the overflowed array (on the stack). That is why if you overflow an array normally, the program will crash, because the return address will be invalidated.
G'n'R
09-19-2003, 04:14 AM
Yo,
Ok, i will take that onboard. Right now i am using a 128byte block and fgets, but will experiment a bit on that....Right now i need to write a read funtion and implements some kind of packet struct to send/recieve...
Thanks for the info... :)
G'n'R
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.