I'm new to websocket programming. I have a client and a server running separately, both compiled from C on Windows 7. The server is running fine, with binding, connection and messaging being made successfully. However, what intrigues me is that it doesn't show up in netstat command listening on the determined port (9002). When the client is initialized, it gets the error 10054, which is "Connection reset by peer". I have already enabled both applications on the Window's firewall, restarted the machine but I haven't been able to figure out what is happening.
Client side code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define PORT 9002
int main(int argc, char *argv[])
{
WSADATA wsa;
printf("\nInitializing Winsock... ");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
WSACleanup();
return EXIT_FAILURE;
}
printf("Initialized.\n");
SOCKET network_socket; // create a socket
// network_socket = socket(domain, type, protocol)
network_socket = socket(AF_INET, SOCK_STREAM, 0);
// domain: AF_INET (IPv4 protocol) / AF_INET6 (Ipv6 protocol)
// type: SOCK_STREAM (TCP protocol) / SOCK_DGRAM (UDP protocol)
// protocol: Internet Protocol (IP) - 0
if (network_socket == INVALID_SOCKET) {
printf("Socket creation error: %d\n", WSAGetLastError());
WSACleanup();
return EXIT_FAILURE;
}
// specify an address for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET; // specifies protocol IPv4
server_address.sin_port = htons(PORT); // specifies port
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
if (inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr) <= 0 ) {
printf("Invalid address / Address not supported: %d\n", WSAGetLastError());
WSACleanup();
return EXIT_FAILURE;
}
// connection
int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
// check for error in the connection
if (connection_status < 0) {
printf("Connection error: %d\n", WSAGetLastError());
WSACleanup();
return EXIT_FAILURE;
}
// Receive data from the server
char buffer[256]; // server response
if ((recv(network_socket, buffer, sizeof(buffer), 0)) == SOCKET_ERROR) {
printf("Receive error: %d\n", WSAGetLastError());
WSACleanup();
return EXIT_FAILURE;
}
// print the server's response
printf("The server sent the data: %s\n", buffer);
// close the socket
closesocket(network_socket);
WSACleanup();
return 0;
}
Server side code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define PORT 9002
int main(int argc, char *argv[])
{
WSADATA wsa;
printf("\nInitializing Winsock... ");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
WSACleanup();
return EXIT_FAILURE;
}
puts("Initialized!");
// create the server socket
SOCKET server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == INVALID_SOCKET) {
printf("Could not create socket: %d\n", WSAGetLastError());
WSACleanup();
return EXIT_FAILURE;
}
// define the server address
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
// bind the socket to our specified IP and port
if ((bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address))) == SOCKET_ERROR) {
printf("Binding failed: %d\n", WSAGetLastError());
WSACleanup();
return EXIT_FAILURE;
}
puts("Binding done!");
// listen for connections
listen(server_socket, 5);
// accept the connection
puts("Waiting for incoming connections...");
SOCKET client_socket;
if (client_socket = accept(server_socket, NULL, NULL) == INVALID_SOCKET) {
printf("Error accepting connections: %d\n", WSAGetLastError());
WSACleanup();
return EXIT_FAILURE;
}
puts("Connection accepted!");
// send the message
char *server_message = "You have reached the server!";
send(client_socket, server_message, sizeof(server_message), 0);
puts("Message sent!");
// close the socket
closesocket(server_socket);
WSACleanup();
return 0;
}
Client side program outputs the following:
Initializing Winsock... Initialized.
Receive error: 10054
Server side program outputs the following:
Initializing Winsock... Initialized!
Binding done!
Waiting for incoming connections...
Connection accepted!
Message sent!
Please, any hints and ideas will be greatly appreciated. Thanks.