简单Echo服务器的实现
1. 要求
实现Echo服务器的客户端/服务器程序,客户端通过网络连接到服务器,并发送任意一串英文信息,服务器端接收信息后,将每个字符转换为大写并回送给客户端显示。
这个项目的主要目的:熟悉C语言的网络编程(Socket),为开发更高级的网络应用打好基础。
2. 服务器端代码
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
#define SERVER_PORT 666
int main(void){
int server_socket,client_socket;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
socklen_t client_address_length;
client_address_length = sizeof(client_address);
/* 1. create server socket
arguments: AF_INET -> use IPv4
SOCK_STREAM -> use tcp
*/
server_socket = socket(AF_INET,SOCK_STREAM,0);
// 2. clear server_address and write server address and server port
bzero(&server_address,sizeof(server_address));
server_address.sin_family = AF_INET; // ipv4
server_address.sin_addr.s_addr = htonl(INADDR_ANY); // designate all ip address
server_address.sin_port = htons(SERVER_PORT); // designate server port
// 3. bind sevrer ip and port to server socket
bind(server_socket,(struct sockaddr *)&server_address,sizeof(server_address));
// 4. start to listen
listen(server_socket,1024);
printf("wait client message .....\n");
// start process client's request
int done = 1;
while(done){
int message_length;
char client_ip_address[64];
char buffer[256];
// start accept client request in order to client socket
client_socket = accept(server_socket,(struct sockaddr *)&client_address,&client_address_length);
// print client's ip and port
printf("client ip: %s\tport: %d\n",inet_ntop(AF_INET,&client_address.sin_addr.s_addr,client_ip_address,sizeof(client_ip_address)),ntohs(client_address.sin_port));
// read client message
// -1 to prevent over the border
message_length = read(client_socket,buffer,sizeof(buffer)-1);
// set string end symbol
buffer[message_length] = '\0';
printf("receive[%d]: %s\n",message_length,buffer);
// lower letter convert to upper letter(a-z -> A-Z)
for(int i = 0; i < message_length; i ++){
buffer[i] = toupper(buffer[i]);
}
// client's message write back to client
write(client_socket,buffer,message_length);
printf("finished len: %d\n",message_length);
// over client's connect
close(client_socket);
}
// over server's connect
close(server_socket);
return 0;
}
3. 客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SERVER_PORT 666
#define SERVER_IP "10.0.0.1"
int main(int argc,char *argv[]){
int client_socket;
char message[256];
struct sockaddr_in server_address;
int n;
char buffer[256];
/*if(argc != 2){
fputs("Error! number of arguments is incorrect!\n",stderr);
exit(1);
}
*/
memset(&server_address,'\0',sizeof(struct sockaddr_in));
server_address.sin_family = AF_INET;
inet_pton(AF_INET,SERVER_IP,&server_address.sin_addr);
server_address.sin_port = htons(SERVER_PORT);
// message = argv[1];
int done = 1;
while(done){
memset(buffer,'\0',sizeof(buffer));
//gets is very dangerous
gets(message);
printf("message:%s\n",message);
client_socket = socket(AF_INET,SOCK_STREAM,0);
connect(client_socket,(struct sockaddr *)&server_address,sizeof(server_address));
write(client_socket,message,strlen(message));
n = read(client_socket,buffer,sizeof(buffer) - 1);
if(n > 0){
buffer[n] = '\0';
printf("receive: %s\n",buffer);
} else{
perror("error!");
}
printf("finished!\n");
close(client_socket);
}
}
4. 结果(在mininet上进行测试)
h1为服务器,h2为客户端。二者均可以通信。
5. 参考资料
https://www.bilibili.com/video/BV14Y411s7yB/?spm_id_from=333.1007.top_right_bar_window_custom_collection.content.click&vd_source=a642bb3ddc5b706845426dc41d73fbda
标签:socket,buffer,address,server,client,简单,服务器,message,Echo
From: https://www.cnblogs.com/gao79135/p/17872619.html