-
server.cpp
#include <iostream> #include <winsock2.h> using namespace std; int main(int argc, char* argv[]) { //初始化WSA WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) { return 0; } //创建套接字 SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (slisten == INVALID_SOCKET) { cout << "create socket error !" << endl; return 0; } //绑定IP和端口 sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(8888); sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR) { cout << "bind error !" << endl; return 0; } //开始监听 if (listen(slisten, 5) == SOCKET_ERROR) { cout << "listen error !" << endl; return 0; } //循环接收数据 SOCKET sClient; sockaddr_in remoteAddr; int nAddrlen = sizeof(remoteAddr); char revData[255]; while (true) { cout << "wait connect..." << endl; sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen); if (sClient == INVALID_SOCKET) { cout << "accept error !" << endl; continue; } cout << "accept a connect!" << inet_ntoa(remoteAddr.sin_addr) << endl; //接收数据 int ret = recv(sClient, revData, 255, 0); if (ret > 0) { revData[ret] = 0x00; cout << revData << endl; } //发送数据 const char * sendData = "hello client\n"; send(sClient, sendData, strlen(sendData), 0); closesocket(sClient); } closesocket(slisten); WSACleanup(); return 0; }
-
client.cpp
#include<winsock2.h> #include<iostream> #include<string> using namespace std; #pragma comment(lib, "ws2_32.lib") int main() { WORD sockVersion = MAKEWORD(2, 2); WSADATA data; if (WSAStartup(sockVersion, &data) != 0) { return 0; } while (true) { SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sclient == INVALID_SOCKET) { cout << "invalid socket!" << endl; return 0; } sockaddr_in serAddr; serAddr.sin_family = AF_INET; serAddr.sin_port = htons(8888); serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR) { //连接失败 cout << "connect error !" << endl; closesocket(sclient); return 0; } string data; cin >> data; const char * sendData; sendData = data.c_str(); //string转const char* /* send()用来将数据由指定的socket传给对方主机 int send(int s, const void * msg, int len, unsigned int flags) s为已建立好连接的socket,msg指向数据内容,len则为数据长度,参数flags一般设0 成功则返回实际传送出去的字符数,失败返回-1,错误原因存于error */ send(sclient, sendData, strlen(sendData), 0); char recData[255]; int ret = recv(sclient, recData, 255, 0); if (ret>0) { recData[ret] = 0x00; cout << recData << endl; } closesocket(sclient); } WSACleanup(); system("pause"); return 0; }
- makefile
lib := -lwsock32 all: server client server: g++ server.cpp -o server.exe ${lib} client: g++ client.cpp -o client.exe ${lib} clean: rm *.exe
udp 客户端测试
#include <stdio.h>
#include<winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char const *argv[])
{
/* 初始化套接字动态库 */
WSADATA data;
if (WSAStartup(MAKEWORD(2, 2), &data) != 0)
{
printf("WSAStartup error\n");
return -1;
}
/* 创建网络套接字 */
SOCKET sclient = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sclient == INVALID_SOCKET)
{
printf("invalid socket!");
return 0;
}
/* 设置服务器地址信息 */
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8888);
serv_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
int serv_addr_len = sizeof(serv_addr);
while (1)
{
int ch = getchar();
char buf[512];
int n = 0;
/* 接收数据 */
if (ch == 'R' || ch == 'W')
{
again:
n = recvfrom(sclient, buf, 512 - 1, 0, (struct sockaddr *)&serv_addr, &serv_addr_len);
if(n < 0)
{
printf("recv error\n");
goto err;
}
buf[n] = '\0';
printf("recv %d bytes: %s\n", n, buf);
/* 循环接收 */
if(ch == 'W')
{
goto again;
}
}
/* 发送数据 */
else if (ch == 'S')
{
n = sendto(sclient, "Hello", 6, 0, (struct sockaddr *)&serv_addr, serv_addr_len);
if (n < 0)
{
printf("sendto error\n");
goto err;
}
}
/* 退出测试 */
else if(ch == 'E')
{
break;
}
}
err:
closesocket(sclient);
WSACleanup();
return 0;
}
网友评论