在本地构造一个服务端和客户端程序,并传输一段简单的信息
参考:《TCP/IP网络编程》尹圣雨,章节1.1
构造服务端代码 hello_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
void error_handling(char *message);
int main(int argc, char *argv[])
{
int serv_sock;
int clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
char message[] = "hello socket";
if(argc != 2)
{
printf("Usage: %s <prot>\n", argv[0]);
exit(1);
}
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
error_handling("socker() 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(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1)
error_handling("bind() error");
if(listen(serv_sock, 5) == -1)
error_handling("listen() error");
socklen_t clnt_addr_size = sizeof(clnt_addr);
clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
if(clnt_sock == -1)
error_handling("accept() error");
write(clnt_sock, message, sizeof(message));
close(clnt_sock);
close(serv_sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
构造客户端代码 hello_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
void error_handling(char *message);
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_in serv_addr;
char message[30];
int str_len;
if(argc != 3)
{
printf("Usage: %s <IP> <prot>\n", argv[0]);
exit(1);
}
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1)
error_handling("socker() error");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1)
error_handling("connect() error");
str_len = read(sock, message, sizeof(message)-1);
if(str_len == -1)
error_handling("read() error");
printf("Message form server: %s \n", message);
close(sock);
return 0;
}
void error_handling(char *message)
{
perror(message);
exit(1);
}
运行:
启用一个终端,先编译后运行服务端程序
(base) yudd@ydduongs-MacBook-Air network % gcc hello_server.c -o hello_server.out
(base) yudd@ydduongs-MacBook-Air network % ./hello_server.out 8888
(base) yudd@ydduongs-MacBook-Air network %
再启用一个终端,运行客户端,端口指定为 8888
(base) yudd@ydduongs-MacBook-Air network % gcc hello_client.c -o hello_client.out
(base) yudd@ydduongs-MacBook-Air network % ./hello_client.out 127.0.0.1 8888
Message form server: hello socket
(base) yudd@ydduongs-MacBook-Air network %
其他
- 如果你也在学些这本书,运行不出结果,提示
connent() error ,可以用perror函数打印出错误 - 如果错误类型是
Connection refused ,可以去查找一下,程序运行的端口号,确认是不是指定的端口。导致问题的原因是使用了htonl函数,就是这serv_addr.sin_port = htonl(atoi(argv[1])); , 换成htons()函数就可以了 - 端口号是16位的,htonl()是进行32转换,导致出错,链接:https://blog.csdn.net/s_whale/article/details/84662557
|