我们有 tcp/ip 应用项目,并在我们的源代码中使用了 winsock2.lib,服务器和客户端功能如下:
服务器:
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include<string>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")
using namespace::std;
int server()
{
string SERVER_ADDRESS= "172.17.11.138";
int PORT_NUMBER = 27015;
sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(PORT_NUMBER);
server.sin_addr.S_un.S_addr=inet_addr(&SERVER_ADDRESS[0]);
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
int iSendResult;
char recvbuf[256];
int recvbuflen = 256;
// Create a SOCKET for connecting to server
ListenSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind( ListenSocket, (struct sockaddr*)&server,sizeof(server));
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
// Echo the buffer back to the sender
iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
}
客户:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
int client()
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
string SERVER_ADDRESS= "172.17.11.138";
char *sendbuf = "this is a test";
char recvbuf[256];
int PORT_NUMBER = 27015;
int iResult;
sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(PORT_NUMBER);
server.sin_addr.S_un.S_addr=inet_addr(&SERVER_ADDRESS[0]);
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
iResult = connect( ConnectSocket,(struct sockaddr*)&server,sizeof(server));
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
iResult = recv(ConnectSocket, recvbuf, 256, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
}
我们编译了上面的代码,服务器和客户端可以相互连接(使用 tcpview 应用程序检查)但是,我们想从服务器发送数据到客户端,winsock2 返回错误 10057(如果我们关闭监听套接字,错误将是 10038 ) 同样,发送函数返回 -1。