codeblocks实现TCP小写转大写
时间: 2023-08-29 10:07:51 浏览: 88
好的,这是一个基于 Code::Blocks 平台的 TCP 小写转大写的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 1024
#define PORT 8888
void error_handling(char *message);
int main(int argc, char *argv[]) {
WSADATA wsaData;
SOCKET serv_sock, clnt_sock;
SOCKADDR_IN serv_addr, clnt_addr;
char message[BUF_SIZE];
int str_len, i;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
error_handling("WSAStartup() error!");
}
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if (serv_sock == INVALID_SOCKET) {
error_handling("socket() error");
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(PORT);
if (bind(serv_sock, (SOCKADDR*)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR) {
error_handling("bind() error");
}
if (listen(serv_sock, 5) == SOCKET_ERROR) {
error_handling("listen() error");
}
int clnt_addr_size = sizeof(clnt_addr);
clnt_sock = accept(serv_sock, (SOCKADDR*)&clnt_addr, &clnt_addr_size);
if (clnt_sock == INVALID_SOCKET) {
error_handling("accept() error");
}
while (1) {
str_len = recv(clnt_sock, message, BUF_SIZE, 0);
if (str_len == 0) {
break;
}
for (i = 0; i < str_len; i++) {
message[i] = toupper(message[i]);
}
send(clnt_sock, message, str_len, 0);
}
closesocket(clnt_sock);
closesocket(serv_sock);
WSACleanup();
return 0;
}
void error_handling(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
```
这个程序接收来自客户端的小写字符串,并将其转换为大写后返回给客户端。你可以在代码中修改端口号以适应你的需求。
阅读全文