C语言实现socket双向通信时服务端运行发生乱码
服务端代码如下#include<stdio.h>#include<winsock2.h>#include<string.h>#pragmacomment(lib,"ws2...
服务端代码如下
#include <stdio.h>
#include <winsock2.h>
#include <string.h>
#pragma comment(lib,"ws2_32.lib")
#define length 1024
DWORD WINAPI FunThread(LPVOID);
int main(int argc, char* argv[])
{//初始化WSA
WORD sockVersion = MAKEWORD(2,2);
WSADATA wsaData;
char message1[length];
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;
} //绑定IP和端口
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;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
char revData[255];
HANDLE hThread=CreateThread(NULL,0,FunThread,0,0,0);
CloseHandle(hThread);
while (true)
{
printf("等待连接...\n");
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
if(sClient == INVALID_SOCKET)
{
printf("accept error !");
continue;
}
printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr)); //接收数据
int ret = recv(sClient, revData, 255, 0);
if(ret > 0)
{
revData[ret] = 0x00;
printf(revData);
}//发送数据
gets(message1);
send(sClient, message1, strlen(message1), 0);
closesocket(sClient);
}
closesocket(slisten);
WSACleanup();
return 0;
}
DWORD WINAPI FunThread(LPVOID)
{
char Message2[length];
int ret;
while (TRUE)
{
SOCKET sClient;
ret = recv(sClient, Message2, length, 0);
Message2[ret] = '\0';
printf("%s\n", Message2);
}
return 0;
}
编译通过 但是运行就不行了 展开
#include <stdio.h>
#include <winsock2.h>
#include <string.h>
#pragma comment(lib,"ws2_32.lib")
#define length 1024
DWORD WINAPI FunThread(LPVOID);
int main(int argc, char* argv[])
{//初始化WSA
WORD sockVersion = MAKEWORD(2,2);
WSADATA wsaData;
char message1[length];
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;
} //绑定IP和端口
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;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
char revData[255];
HANDLE hThread=CreateThread(NULL,0,FunThread,0,0,0);
CloseHandle(hThread);
while (true)
{
printf("等待连接...\n");
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
if(sClient == INVALID_SOCKET)
{
printf("accept error !");
continue;
}
printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr)); //接收数据
int ret = recv(sClient, revData, 255, 0);
if(ret > 0)
{
revData[ret] = 0x00;
printf(revData);
}//发送数据
gets(message1);
send(sClient, message1, strlen(message1), 0);
closesocket(sClient);
}
closesocket(slisten);
WSACleanup();
return 0;
}
DWORD WINAPI FunThread(LPVOID)
{
char Message2[length];
int ret;
while (TRUE)
{
SOCKET sClient;
ret = recv(sClient, Message2, length, 0);
Message2[ret] = '\0';
printf("%s\n", Message2);
}
return 0;
}
编译通过 但是运行就不行了 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
1个回答
展开全部
int ret = recv(sClient, revData, 255, 0); recv之前要把buffer清空
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询