#include "stdafx.h"
#include <Winsock2.h>
#include <cstdio>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
int main()
{
WSADATA wsdata = {0};
WSAStartup(MAKEWORD(2, 2), &wsdata); //Winsock服务的初始化,这句话一定要放在前面
SOCKET socketServer = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addserver;
addserver.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//INADDR_ANY表示任何IP
addserver.sin_family = AF_INET;
addserver.sin_port = htons(6000);
auto res=bind(socketServer, (SOCKADDR*)&addserver, sizeof(SOCKADDR));
listen(socketServer, 5);
printf("服务器已启动:
监听中...
");
SOCKET socketClient;
SOCKADDR_IN addClient;
int len = sizeof(SOCKADDR);
char charsendBuf[100];//发送至客户端的字符串
char charrecvBuf[100];//接受客户端返回的字符串
//会阻塞进程,直到有客户端连接上来为止
socketClient = accept(socketServer, (SOCKADDR*)&addClient, &len);
//接收并打印客户端数据
recv(socketClient, charrecvBuf, 100, 0);
printf("%s
", charrecvBuf);
//关闭socket
closesocket(socketClient);
WSACleanup();
}
客户端:
#include "stdafx.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1
#include <Winsock2.h>
#include <cstdio>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
int main()
{
WSADATA wsaData;
SOCKET sockClient;//客户端Socket
sockaddr_in addrServer;//服务端地址
WSAStartup(MAKEWORD(2, 2), &wsaData);
//新建客户端socket
sockClient = socket(AF_INET, SOCK_STREAM, 0);
//定义要连接的服务端地址
addrServer.sin_addr.s_addr = inet_addr("127.0.0.1");//目标IP(127.0.0.1是回送地址)
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(6000);//连接端口6000
//连接到服务端
connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
auto err = GetLastError();
std::cout << err;
//发送数据
char message[20] = "HelloSocket!";
send(sockClient, message, strlen(message) + 1, 0);
//关闭socket
closesocket(sockClient);
WSACleanup();
return 0;
}
服务器端编程的步骤:
1:加载套接字库,创建套接字(WSAStartup()/socket());
2:绑定套接字到一个IP地址和一个端口上(bind());
3:将套接字设置为监听模式等待连接请求(listen());
4:请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept());
5:用返回的套接字和客户端进行通信(send()/recv());
6:返回,等待另一连接请求;
7:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。
客户端编程的步骤:
1:加载套接字库,创建套接字(WSAStartup()/socket());
2:向服务器发出连接请求(connect());
3:和服务器端进行通信(send()/recv());
4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。