|
-
February 20th, 2008, 10:46 PM
#1
Socket's won't work remotely.
I have been working on a server/client program now for a few weeks, and they work flawlessly together by binding the client to 127.0.0.1, or the localhost. But, for some reason, the client/server cannot connect by having the client on one computer (ip address) and a server on another. Shouldn't this just automatically work, or is there something I'm missing? This definitely has something to do with either socket() (WSASocket()) or accept(), but I cannot distinguish what...
Here is the broken down version of the client (this is in a function, so client_ip and port variables are sent through the function, these work properly.):
Code:
WSAData wsdata;
WORD wsver = MAKEWORD(2, 0);
int startup = WSAStartup(wsver, &wsdata); implement
if(startup != 0){
std::cout << "Startup failed: " << WSAGetLastError();
WSACleanup();
return false;
}
std::cout << "Connected to " << connect_ip << ":" << port << "\n";
// SOCKET kSock = socket(AF_INET, SOCK_STREAM, 0); // not sure which, this or one underneath...
SOCKET kSock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
if(kSock == INVALID_SOCKET){
std::cout << "Socket init failed";
return -1;
}
std::cout << "Socket initialized\n";
sockaddr_in sin;
sin.sin_port = htons(port); //Connect to port 80
sin.sin_addr.s_addr = inet_addr(connect_ip.c_str()); //Connect to localhost INADDR_ANY
sin.sin_family = AF_INET;
if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
std::cout << "Connect failed, error: " << WSAGetLastError(); //Returns error code
WSACleanup(); //Cleanup the library
return false;
} else {
.... and so on ....
Server:
Code:
WSADATA wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
if ((fd = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0)) == -1) {
cout << "Socket error...\n";
return false;
} else {
cout << "Socket intialized...\n";
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
memset(&(server.sin_zero), '/0', 8);
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(BOOL)) < 0) {
cout << "Unbind error...\n";
} else {
cout << "Socket unbound...\n";
}
if (bind(fd,(struct sockaddr *)&server,sizeof(struct sockaddr)) == -1) {
cout << "Error Code: " << WSAGetLastError();
cout << "\nBind failed...\n";
return false;
} else {
cout << "Bind works...\n";
}
if (listen(fd, BACKLOG) == -1) { // BACKLOG SOMAXCONN
cout << WSAGetLastError();
cout << "\nListen failed...\n";
return false;
} else {
cout << "Listen works...\n";
}
sin_size = sizeof(struct sockaddr_in);
cout << "Waiting for client...\n";
if((fd2 = accept(fd, (struct sockaddr *)&client,&sin_size)) == -1) {
cout << "Error Code: " << WSAGetLastError();
cout << "\nAccept failed...\n";
return false;
} else {
cout << "Connection accepted...\n";
}
... goes on and on ....
again, this works by binding connecting as client to 127.0.0.1 on the chosen port, but this will not work if the programs are each on a different computer (ip). Any suggestions of what I am doing wrong?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|