#define _WINSOCK_DEPRECATED_NO_WARNINGS #include <stdio.h> #include <Winsock2.h> #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 #endif #pragma comment(lib,"ws2_32.lib") void main() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(1, 1); 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(6000); connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)); send(sockClient, "hello", strlen("hello") + 1, 0); char recvBuf[50]; recv(sockClient, recvBuf, 50, 0); printf("%s\n", recvBuf); closesocket(sockClient); WSACleanup(); }这段代码我的编译器vs编译后输出烫烫烫烫烫烫烫烫
时间: 2024-03-05 13:50:06 浏览: 162
这个问题可能是因为你发送的字符串没有以'\0'结尾,所以接收端无法正确地解析它。你可以尝试在发送时将字符串以如下方式发送:
```
send(sockClient, "hello\0", strlen("hello") + 1, 0);
```
另外,你的main函数的返回类型应该是`int`而不是`void`。你可以将它改为:
```
int main() {
// your code here
return 0;
}
```
最后,对于网络编程,建议你在编写完整个程序后再进行调试,以确保所有的网络连接和数据传输都是正确的。
相关问题
客户端代码 #define _WINSOCK_DEPRECATED_NO_WARNINGS #include <stdio.h> #include <Winsock2.h> #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 #endif #pragma comment(lib,"ws2_32.lib") int main() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(1, 1); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { return -1; } if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) { WSACleanup(); return -1; } 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(6000); connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)); send(sockClient, "hello\0", strlen("hello") + 1, 0); char recvBuf[50]; recv(sockClient, recvBuf, 50, 0); printf("%s\n", recvBuf); closesocket(sockClient); WSACleanup(); return 0;} 服务器端:#define _WINSOCK_DEPRECATED_NO_WARNINGS #include <stdio.h> #include <Winsock2.h> #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 #endif #pragma comment(lib,"ws2_32.lib") int main() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { return 1; } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { WSACleanup(); return 1; } SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0); int optval = 1; setsockopt(sockSrv, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof(optval)); SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(6000); bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)); listen(sockSrv, 5); SOCKADDR_IN addrClient; int len = sizeof(SOCKADDR); while (1) { SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len); char sendBuf[50]; printf(sendBuf, "Welcome %s to here!", inet_ntoa(addrClient.sin_addr)); send(sockConn, sendBuf, strlen(sendBuf) + 1, MSG_NOSIGNAL); char recvBuf[50]; recv(sockConn, recvBuf, 50, 0); printf("%s\n", recvBuf); closesocket(sockConn); } WSACleanup(); return 0;} 如何修改代码改成可以一直聊天的 不要预输入进去的 要我自己在客户端进行打字操作
要实现客户端和服务器之间的实时聊天,需要在客户端代码中添加一个循环来不断读取用户输入的消息,并将其发送给服务器。同时,在服务器端代码中也需要添加一个循环来不断接收客户端发送的消息,并将其广播给所有连接的客户端。
下面是修改后的客户端代码:
```
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <Winsock2.h>
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#pragma comment(lib,"ws2_32.lib")
int main() {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return -1;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return -1;
}
// 创建套接字并连接到服务器
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(6000);
connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
char sendBuf[50], recvBuf[50];
while (1) {
// 读取用户输入的消息并发送给服务器
printf("Input message to send: ");
fgets(sendBuf, 50, stdin);
send(sockClient, sendBuf, strlen(sendBuf) + 1, 0);
// 接收服务器的响应并打印出来
recv(sockClient, recvBuf, 50, 0);
printf("Received from server: %s\n", recvBuf);
}
closesocket(sockClient);
WSACleanup();
return 0;
}
```
下面是修改后的服务器端代码:
```
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <Winsock2.h>
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#pragma comment(lib,"ws2_32.lib")
int main() {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return 1;
}
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
WSACleanup();
return 1;
}
// 创建套接字并绑定到本地地址和端口
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
int optval = 1;
setsockopt(sockSrv, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof(optval));
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
// 监听连接请求并接受客户端连接
listen(sockSrv, 5);
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
SOCKET sockConn;
fd_set fdReads;
while (1) {
// 使用 select() 函数实现非阻塞接收客户端连接
FD_ZERO(&fdReads);
FD_SET(sockSrv, &fdReads);
int ret = select(sockSrv + 1, &fdReads, NULL, NULL, NULL);
if (ret < 0) {
break;
}
if (FD_ISSET(sockSrv, &fdReads)) {
sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
char sendBuf[50];
printf(sendBuf, "Welcome %s to here!\n", inet_ntoa(addrClient.sin_addr));
send(sockConn, sendBuf, strlen(sendBuf) + 1, MSG_NOSIGNAL);
}
// 使用 select() 函数实现非阻塞接收客户端消息
FD_ZERO(&fdReads);
FD_SET(sockConn, &fdReads);
ret = select(sockConn + 1, &fdReads, NULL, NULL, NULL);
if (ret < 0) {
break;
}
if (FD_ISSET(sockConn, &fdReads)) {
char recvBuf[50];
recv(sockConn, recvBuf, 50, 0);
printf("Received from client: %s\n", recvBuf);
// 将客户端发送的消息广播给所有连接的客户端
for (SOCKET i = 0; i < FD_SETSIZE; i++) {
if (FD_ISSET(i, &fdReads)) {
send(i, recvBuf, strlen(recvBuf) + 1, MSG_NOSIGNAL);
}
}
}
}
closesocket(sockSrv);
WSACleanup();
return 0;
}
```
为什么我运行了这段代码以后却什么都不显示呢#define _WINSOCK_DEPRECATED_NO_WARNINGS #include <stdio.h> #include <Winsock2.h> #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 #endif #pragma comment(lib,"ws2_32.lib") int main() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { return 1; } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { WSACleanup(); return 1; } SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0); int optval = 1; setsockopt(sockSrv, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof(optval)); SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(6000); bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)); listen(sockSrv, 5); SOCKADDR_IN addrClient; int len = sizeof(SOCKADDR); while (1) { SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len); char sendBuf[50]; printf(sendBuf, "Welcome %s to here!", inet_ntoa(addrClient.sin_addr)); send(sockConn, sendBuf, strlen(sendBuf) + 1, MSG_NOSIGNAL); char recvBuf[50]; recv(sockConn, recvBuf, 50, 0); printf("%s\n", recvBuf); closesocket(sockConn); } WSACleanup(); return 0;}
这段代码是一个简单的TCP Server,它监听6000端口,当客户端连接到该端口时,它会向客户端发送一条欢迎消息并等待接收客户端的消息。
在代码中,您没有在发送欢迎消息之前初始化sendBuf数组,因此它可能包含任意值,这会导致printf函数输出未定义的结果。因此,您应该将sendBuf数组初始化为一个空字符串,例如:
```
char sendBuf[50] = {0};
```
另外,您可能需要在代码中添加一些错误检查以确保sockSrv和sockConn的创建和绑定都成功。如果创建和绑定失败,则程序将无法正常工作。
阅读全文