// WSA_Server.cpp
// By Baozhong Chengㄛ02-21-2010
// Windows Socket API Server side program
// NOTE:
// 1. add ws2_32.lib into project properties "link -> input -> additional library"
// 2. using WSA_Server.cpp instead of WSA_Server.c to declare variable in
// the middle of a function, make it more readable
/* ------------------------------------------------------------------------------------
1. Initialize WSA 每 WSAStartup().
2. Create a socket 每 socket().
3. Bind the socket 每 bind().
4. Listen on the socket 每 listen().
5. Accept a connection 每 accept(), connect().
6. Send and receive data 每 recv(), send(), recvfrom(), sendto().
7. Disconnect 每 closesocket().
8. Clearn up WSA - WSACleanup();
------------------------------------------------------------------------------------- */
#include <stdio.h>
#include <winsock2.h>
int main() {
WORD wVersionRequested;
WSADATA wsaData;
int wsaerr;
// ----------------- 1. Initialize WSA 每 WSAStartup(). -------------------
wVersionRequested = MAKEWORD(2, 2); // Using Winsock version request 2.2
wsaerr = WSAStartup(wVersionRequested, &wsaData);
if (wsaerr != 0) {
printf("The Winsock dll not found!\n");
return 0;
}
// ----------------- 2. Create a socket 每 socket(). -------------------
SOCKET m_socket;
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 0;
}
// ----------------- 3. Bind the socket 每 bind(). -------------------
sockaddr_in service; // Create a sockaddr_in object and set its values.
service.sin_family = AF_INET; // AF_INET is the Internet address family.
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(55555); // 55555 is the port number
if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR) {
printf("bind() failed: %ld.\n", WSAGetLastError());
closesocket(m_socket);
return 0;
}
// ----------------- 4. Listen on the socket 每 listen(). -------------------
// Call the listen function, passing the created socket and the maximum number of allowed
// connections to accept as parameters. Check for general errors.
if (listen( m_socket, 1) == SOCKET_ERROR) {
printf("listen(): Error listening on socket %ld.\n", WSAGetLastError());
return 0;
}
// ----------------- 5. Accept a connection 每 accept(), connect(). -------------------
SOCKET AcceptSocket; // Create a temporary SOCKET object for accepting connections.
printf("Server is ready...run your client program...\n");
while (1) {
AcceptSocket = SOCKET_ERROR;
while (AcceptSocket == SOCKET_ERROR)
AcceptSocket = accept(m_socket, NULL, NULL);
printf("Server: Client Connected!\n");
m_socket = AcceptSocket;
break;
}
// ----------------- 6. Send and receive data 每 recv(), send(), recvfrom(), sendto(). -------------------
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[200] = "This string is a test data from server";
char recvbuf[200] = ""; // initialize to empty data...
// Send some test string to client...
printf("Server: Sending some test data to client...\n");
bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0);
if (bytesSent == SOCKET_ERROR)
printf("Server: send() error %ld.\n", WSAGetLastError());
// Receives some test string from client...and client
bytesRecv = recv(m_socket, recvbuf, 200, 0);
if (bytesRecv == SOCKET_ERROR)
printf("Server: recv() error %ld.\n", WSAGetLastError());
else {
printf("Server: recv() is OK.\n");
printf("Server: Received data is: \"%s\"\n", recvbuf);
printf("Server: Bytes received: %ld.\n", bytesRecv);
}
// ----------------- 7. Disconnect 每 closesocket(). -------------------
closesocket(m_socket);
// ----------------- 8. Clearn up WSA - WSACleanup(); -------------------
WSACleanup();
return 0;
}