请大神帮我把这段socket编程的C语言代码代称C++代码吧
答案:3 悬赏:40 手机版
解决时间 2021-03-31 07:30
- 提问者网友:树红树绿
- 2021-03-30 23:16
请大神帮我把这段socket编程的C语言代码代称C++代码吧
最佳答案
- 五星知识达人网友:人间朝暮
- 2021-03-31 00:44
这是大致的结构,你这给的是图片,要是是文本就好了,不然要打这么多字。。。
class Server {
public:
Server(int family, int type, int proto,
int port = 9000, int que_size = 5)
: _family(family), _type(type), _protocol(proto),
_port(port), _que_size(que_size) {}
~Server() { close(_serv_sock); }
bool init(); // 初始化socket
void run(); // 运行服务程序
private:
int serve(int client_sock);
int _serv_sock; // 正在监听的端口
int _family; // 地址族
int _type; // 类型:SOCK_STREAM SOCK_DGRAM
int _protocol; // 协议:IPPROTO_TCP IPPROTO_UDP
int _port; // 端口号
int _que_size; // 监听队列的长度
};
bool Server::init()
{
_serv_sock = socket(_family, _type, _protocol);
struct sockaddr_in serv_addr;
serv_addr.sin_family = _family;
serv_addr.sin_port = htons(_port);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(server_addr.sin_zero), 0, 8);
if(bind(_serv_sock, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) {
printf("Bad bind
");
perror("details");
return false;
}
if(listen(_serv_sock, _que_size) == -1) {
printf("Bad listen
");
exit(1);
}
printf("Accepting connections ...
");
return true;
}
void Server::run()
{
struct sockaddr_in clientAddr;
int len = sizeof(struct sockaddr_in);
while(true) {
int client_sock = accept(_serv_sock, (struct sockaddr *)&clientAddr, &len);
int pid = fork();
if(pid == 0) { // 如果是子进程
int exit_code = serve(client_sock);
close(client_sock);
exit(EXIT_SUCCESS);
}
close(client_sock);
}
}
int Server::serv(int client_sock)
{
while(true) {
int len;
char *head_str = "you said: ";
if(read(client_sock, &len, sizeof(len)) == 0)
return 0;
char *text = new char[len];
read(client_sock, text, len);
int n = strlen(text); // 避免多次调用 strlen
for(int i = 0; i < n; i++)
text[i] = toupper(text[i]);
char *text2 = new char[len + strlen(head_str) + 1];
strcpy(text2, head_str);
strcat(text2, text);
if(send(client_sock, text2, strlen(text2), 0) == -1) {
perror("call to send");
delete[] text; // 在这里也应释放内存
delete[] text2;
exit(1);
}
delete[] text;
delete[] text2;
}
}
int main()
{
Server serv(AF_INET, SOCK_STREAM, 0);
if(serv.init())
serv.run();
return 0;
}
class Server {
public:
Server(int family, int type, int proto,
int port = 9000, int que_size = 5)
: _family(family), _type(type), _protocol(proto),
_port(port), _que_size(que_size) {}
~Server() { close(_serv_sock); }
bool init(); // 初始化socket
void run(); // 运行服务程序
private:
int serve(int client_sock);
int _serv_sock; // 正在监听的端口
int _family; // 地址族
int _type; // 类型:SOCK_STREAM SOCK_DGRAM
int _protocol; // 协议:IPPROTO_TCP IPPROTO_UDP
int _port; // 端口号
int _que_size; // 监听队列的长度
};
bool Server::init()
{
_serv_sock = socket(_family, _type, _protocol);
struct sockaddr_in serv_addr;
serv_addr.sin_family = _family;
serv_addr.sin_port = htons(_port);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(server_addr.sin_zero), 0, 8);
if(bind(_serv_sock, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) {
printf("Bad bind
");
perror("details");
return false;
}
if(listen(_serv_sock, _que_size) == -1) {
printf("Bad listen
");
exit(1);
}
printf("Accepting connections ...
");
return true;
}
void Server::run()
{
struct sockaddr_in clientAddr;
int len = sizeof(struct sockaddr_in);
while(true) {
int client_sock = accept(_serv_sock, (struct sockaddr *)&clientAddr, &len);
int pid = fork();
if(pid == 0) { // 如果是子进程
int exit_code = serve(client_sock);
close(client_sock);
exit(EXIT_SUCCESS);
}
close(client_sock);
}
}
int Server::serv(int client_sock)
{
while(true) {
int len;
char *head_str = "you said: ";
if(read(client_sock, &len, sizeof(len)) == 0)
return 0;
char *text = new char[len];
read(client_sock, text, len);
int n = strlen(text); // 避免多次调用 strlen
for(int i = 0; i < n; i++)
text[i] = toupper(text[i]);
char *text2 = new char[len + strlen(head_str) + 1];
strcpy(text2, head_str);
strcat(text2, text);
if(send(client_sock, text2, strlen(text2), 0) == -1) {
perror("call to send");
delete[] text; // 在这里也应释放内存
delete[] text2;
exit(1);
}
delete[] text;
delete[] text2;
}
}
int main()
{
Server serv(AF_INET, SOCK_STREAM, 0);
if(serv.init())
serv.run();
return 0;
}
全部回答
- 1楼网友:思契十里
- 2021-03-31 02:26
你急不急,不急的话我给你弄
- 2楼网友:怀裏藏嬌
- 2021-03-31 01:23
全是图片, 你想累死我, 把文本复制给来.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯