I’m a novice/beginner programmer having problems getting some simple client/server C code working. My end goal is to send a ‘stream’ of azimuth/elevation data from a server to a client, and then convert that data as it is received (it will just be a division, but I don’t really know how to do this either) into position data for a pan/tilt unit, and then output the converted data via serial to the pan/tilt head. (I’ll likely be back to ask about that later…)
Right now I’m just trying to figure out how to get the data sent and received. I grabbed code from this website. http://www.tenouk.com/Winsock/Winsock2example3.html. I had to move a few declarations around to get the code to compile.
I’m using Windows 7 and VS2010 professional on the client pc. There is no router in between the client and server (they’re directly connected via ethernet).
Using the debugger, I found that I’m getting hung up at this point.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("12.266.66.255");
clientService.sin_port = htons(55555);
if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
printf("Client: connect() - Failed to connect.\n");
WSACleanup();
return 0;
}
else
{
printf("Client: connect() is OK.\n");
printf("Client: Can start sending and receiving data...\n");
}
I always get the “failed to connect” message, and I’m not sure why. I am using the correct IP address of the host computer (I changed it before putting on here).
If this is a bad example to use, I’m open to starting over with another example. I’ve tried several of the ‘echo’ examples commonly found online, and I’m getting similar problems. I can give more info on my overall program goals as well if that would help. The rest of the client code is below. I’m using the server code (with declarations moved around) from the same link. Thanks.
int main()
{
int m_socket;
struct sockaddr_in clientService;
int bytesSent;
int bytesRecv = SOCKET_ERROR;
// Be careful with the array bound, provide some checking mechanism...
char sendbuf[200] = "This is a test string from client";
char recvbuf[200] = "";
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Client: Error at WSAStartup().\n");
else
printf("Client: WSAStartup() is OK.\n");
// Create a socket
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET)
{
printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 0;
}
else
printf("Client: socket() is OK.\n");
// Connect to a server.
// Just test using the localhost, you can try other IP address
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("12.233.21.254");
clientService.sin_port = htons(55555);
if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
printf("Client: connect() - Failed to connect.\n");
WSACleanup();
return 0;
}
else
{
printf("Client: connect() is OK.\n");
printf("Client: Can start sending and receiving data...\n");
}
// Send and receive data.
// Receives some test string to server...
while(bytesRecv == SOCKET_ERROR)
{
bytesRecv = recv(m_socket, recvbuf, 200, 0);
if (bytesRecv == 0 || bytesRecv == WSAECONNRESET)
{
printf("Client: Connection Closed.\n");
break;
}
if (bytesRecv < 0)
return 0;
else
{
printf("Client: recv() is OK.\n");
printf("Client: Received data is: \"%s\"\n", recvbuf);
printf("Client: Bytes received is: %ld.\n", bytesRecv);
}
}
// Sends some test data to server...
bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0);
if(bytesSent == SOCKET_ERROR)
printf("Client: send() error %ld.\n", WSAGetLastError());
else
{
printf("Client: send() is OK - Bytes sent: %ld\n", bytesSent);
printf("Client: The test string sent: \"%s\"\n", sendbuf);
}
WSACleanup();
return 0;
}
Your original code (after I changed the IP and port, obviously) connected to my web-server just fine, but I did tweak it a bit (below).
While it might be a bit much, CSocketServer contains a wealth of good ol’ WinSock code that’s been tried and held true.
Anyway, this code connected to my local web server, sent a rudimentary request and received a response.