孙鑫的VC++网络编程问题
#include <WinSock2.h>#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (0 != err)
{
return;
}
if (LOBYTE(wsaData.wVersion != 1) || HIBYTE(wsaData.wVersion) != 1)
{
WSACleanup();
return;
}
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6253);
bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
listen(sockSrv, 5);
SOCKADDR_IN addrClient;
int len;
len = sizeof(SOCKADDR);
while (1)
{
SOCKET sockConn = accept(sockSrv, (sockaddr*)&addrClient, &len);
char sendBuf = {0};
sprintf(sendBuf, "welcome %s to http://www.sunxin.org", inet_ntoa(addrClient.sin_addr));
send(sockConn, sendBuf, strlen(sendBuf) + 1, 0);
char recvBuf = {0};
recv(sockConn, recvBuf, 100, 0);
printf("%s\n", recvBuf);
closesocket(sockConn);
}
}
这是服务器端代码 本帖最后由 方小韩丶 于 2018-11-9 22:38 编辑
#include <WinSock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1, 1);
int err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
return;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
WSACleanup();
return;
}
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6253);
connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
char recvBuf = {0};
recv(sockClient, recvBuf, 100, 0);
printf("%s\n", recvBuf);
send(sockClient, "This is ", strlen("This is ") + 1, 0);
closesocket(sockClient);
WSACleanup();
}
这是客户端的 两个运行之后只有请按任意键继续是什么原因?再就是运行服务端的EXE秒退 感觉并不是一直循环啊 @BngThea 召唤大神,这种问题一般人答不了 //if(LOBYTE(wsaData.wVersion != 1) || HIBYTE(wsaData.wVersion) != 1)
if((LOBYTE(wsaData.wVersion) != 1) || (HIBYTE(wsaData.wVersion) != 1))
{
WSACleanup();
return;
} if(LOBYTE(wsaData.wVersion != 1) || HIBYTE(wsaData.wVersion) != 1)
00DC5070 0F B7 85 5C FE FF FF movzx eax,word ptr
00DC5077 83 F8 01 cmp eax,1
00DC507A 74 0C je main+78h (0DC5088h)
00DC507C C7 85 9C FB FF FF 01 00 00 00 mov dword ptr ,1
00DC5086 EB 0A jmp main+82h (0DC5092h)
00DC5088 C7 85 9C FB FF FF 00 00 00 00 mov dword ptr ,0
00DC5092 8B 8D 9C FB FF FF mov ecx,dword ptr
00DC5098 81 E1 FF 00 00 00 and ecx,0FFh
00DC509E 0F B6 D1 movzx edx,cl
00DC50A1 85 D2 test edx,edx
00DC50A3 75 17 jne main+0ACh (0DC50BCh)
00DC50A5 0F B7 85 5C FE FF FF movzx eax,word ptr
00DC50AC C1 E8 08 shr eax,8
00DC50AF 25 FF 00 00 00 and eax,0FFh
00DC50B4 0F B6 C8 movzx ecx,al
00DC50B7 83 F9 01 cmp ecx,1
00DC50BA 74 14 je main+0C0h (0DC50D0h)
{
WSACleanup();
00DC50BC 8B F4 mov esi,esp
00DC50BE FF 15 FC B0 DC 00 call dword ptr
00DC50C4 3B F4 cmp esi,esp
00DC50C6 E8 70 C0 FF FF call __RTC_CheckEsp (0DC113Bh)
return;
00DC50CB E9 AA 01 00 00 jmp main+26Ah (0DC527Ah)
}
if((LOBYTE(wsaData.wVersion) != 1) || (HIBYTE(wsaData.wVersion) != 1))
001B5070 0F B7 85 5C FE FF FF movzx eax,word ptr
001B5077 25 FF 00 00 00 and eax,0FFh
001B507C 0F B6 C8 movzx ecx,al
001B507F 83 F9 01 cmp ecx,1
001B5082 75 17 jne main+8Bh (01B509Bh)
001B5084 0F B7 85 5C FE FF FF movzx eax,word ptr
001B508B C1 E8 08 shr eax,8
001B508E 25 FF 00 00 00 and eax,0FFh
001B5093 0F B6 C8 movzx ecx,al
001B5096 83 F9 01 cmp ecx,1
001B5099 74 14 je main+9Fh (01B50AFh)
{
WSACleanup();
001B509B 8B F4 mov esi,esp
001B509D FF 15 FC B0 1B 00 call dword ptr
001B50A3 3B F4 cmp esi,esp
001B50A5 E8 91 C0 FF FF call __RTC_CheckEsp (01B113Bh)
return;
001B50AA E9 AA 01 00 00 jmp main+249h (01B5259h)
} 人造人 发表于 2018-11-9 23:41
...可以了 谢谢老板
页:
[1]