Original Post
I have never coded anything involving networking or multiplayer before, but I wanted to try. So I read some of the articles here on gamedev, looking for basic tutorials on programming using winsock. I found some, and they were useful. However, I figured I'd try to code something very simple, and it doesn't work.
The idea was this. The program gives you two options, client and server.
As a server, you wait for 1 client to connect. You then receive a prompt asking for a text string. Enter it, and it is sent to the client. Done.
As a client, you connect to the server (connecting to localhost is hard-coded in) and wait for a message from the server. When you receive it, display it. Done.
The program uses a blocking server and TCP/IP.
Or at least that's how it's supposed to work. Instead, what happens is this: I start one instance of the program, and tell it to be a server. It waits. Then I start a second instance, and tell it to be a client. It waits. However, the prompt has now appeared on the server asking for the string. I enter it, and the program finishes. I then check the client. It has just output a bunch of characters to the screen, and the program is now done. So far, everything sounds like its supposed to. However, the characters that I get on the client are not the characters that I input on the server. In fact, when I step through that section of the code, the contents of the buffer before my recv(...) call are exactly the same as after it. Something, obviously, is wrong.
I'm about to paste the code here, and I understand it's a little much. Hopefully, though, somebody (you?) can point out my mistake. Chances are it's pretty simple.
Any help would be greatly appreciated. Thanks in advance.
Edited by - TerranFury on January 18, 2002 9:38:10 PM
#pragma comment (lib, "ws2_32.lib")
#include <winsock2.h>
#include <windows.h>
#include <string.h>
//#include <conio.h>
#include <iostream>
using namespace std;
const int PORT_NUM = 5513; //I just arbitrarily chose this
const int BUFFER_SIZE = 128;
bool initWinsock()
{
WSADATA w; // used to store information about WinSock version
int error = WSAStartup (0x0202, &w); // Fill in w
if (error)
{ // there was an error
return false;
}
if (w.wVersion != 0x0202)
{ // wrong WinSock version!
WSACleanup (); // unload ws2_32.dll
return false;
}
return true;
}
int server()
{
cout << "SERVER\n";
const int MAX_CLIENTS = 1;
// Create socket:
// SOCK_STREAM = TCP
// SOCK_DGRAM = UDP
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
cout << "Could not create socket.\n";
return 1;
}
//Bind socket (only for servers)
sockaddr_in addr; // the address structure for a TCP socket
addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (PORT_NUM); // Assign port 80 to this socket
addr.sin_addr.s_addr = htonl (INADDR_ANY); // No destination
if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
{ // error
cout << "Could not bind socket.\n";
return 1; // quit
}
//Server: Listen to port
if (listen(s,MAX_CLIENTS)==SOCKET_ERROR)
{
// error! unable to listen
cout << "Could not listen to port.\n";
return 1;
}
//Wait for client and accept him:
SOCKET client; // socket handles to clients
sockaddr client_sock; // info on client sockets
int addr_size = sizeof(client_sock);
client =
accept (s, &client_sock, &addr_size);
if (client == INVALID_SOCKET)
{
// error accepting connection
cout << "Could not accept connection\n";
return 1;
}
else
{
// start a thread that will communicate with client
}
//Accept and send data
char buffer[BUFFER_SIZE];
cout << "Enter any string: ";
cin >> buffer;
//Send data:
if(send (s, buffer, strlen(buffer), 0) == SOCKET_ERROR)
{
cout << "Error sending.\nError code: "
<< WSAGetLastError();
return 1;
}
shutdown (s, SD_SEND); // s cannot send anymore
// you should check to see if any last data has arrived here
closesocket (s); // close
return 0;
}
int client()
{
cout << "CLIENT\n";
//Create client socket
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP );
if(s == INVALID_SOCKET)
{
cout << "Could not create socket.\n";
return 1;
}
//Try to connect with socket s
sockaddr_in target;
target.sin_family = AF_INET; // address family Internet
target.sin_port = htons (PORT_NUM); // set server’s port number
target.sin_addr.s_addr = inet_addr ("127.0.0.1"); // set server’s IP (originally 52.123.72.251)
if (connect(s, (LPSOCKADDR)⌖, sizeof(target)) == SOCKET_ERROR)
{
// an error connecting has occurred!
cout << "Could not connect\n.";
return 1;
}
//Receive data
char buffer2[BUFFER_SIZE];
Sleep(100);
if(recv (s, buffer2, sizeof(buffer2), 0) == SOCKET_ERROR)
{
cout << "Error receiving.\nError code: "
<< WSAGetLastError();
return 1;
}
cout << buffer2 << endl;
shutdown (s, SD_RECEIVE); // s cannot receive anymore
// you should check to see if any last data has arrived here
closesocket (s); // close
return 0;
}
int main()
{
if(!initWinsock())
{
cout << "Could not initialize winsock\n";
return 1;
}
int choice;
cout << "Client (1) or server (0)? ";
cin >> choice;
if(choice == 0)
server();
else if(choice == 1)
client();
WSACleanup();
return 0;
}
|