1 Star 0 Fork 1

yeyanhong/windows-socket

forked from youquc/windows-socket 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.c 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
easydots 提交于 2021-03-04 04:35 . init
#include<stdio.h>
#include<stdlib.h>
#include<winsock2.h>
#include<windows.h>
int main() {
//ʼ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) {
printf("socket error");
return 0;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(slisten, (LPSOCKADDR)&sin,sizeof(sin)) == SOCKET_ERROR) {
printf("bind error");
}
if(listen(slisten, 5) == SOCKET_ERROR) {
printf("listen error");
return 0;
}
SOCKET sClient;
struct sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
printf("ȴ...\n");
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
while(1) {
char revData[255];
if(sClient == INVALID_SOCKET) {
printf("accept error");
continue;
}
// printf("ܵ:%s \n", inet_ntoa(remoteAddr.sin_addr));
int ret = recv(sClient, revData, 255, 0);
if(ret > 0) {
revData[ret] = 0x00;
printf("recv:%s\n",revData);
}
char *sendData = "send ok!(from server)\n";
send(sClient, sendData, strlen(sendData), 0);
}
closesocket(sClient);
closesocket(slisten);
WSACleanup();
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/ye-yanhong/windows-socket.git
git@gitee.com:ye-yanhong/windows-socket.git
ye-yanhong
windows-socket
windows-socket
master

搜索帮助