Linux Socket編程:基于TCP/IP的客戶端與服務(wù)端通訊實(shí)例
服務(wù)端代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
int serv_sock, clnt_sock;
struct sockaddr_in serv_addr, clnt_addr;
socklen_t clnt_addr_size;
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(1);
}
serv_sock = socket(AF_INET, SOCK_STREAM, 0);
if (serv_sock < 0) {
printf("sock() error\n");
exit(1);
}
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]));
int optval = 1;
// setting address and port reusable
setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
printf("bind() error\n");
exit(1);
}
if (listen(serv_sock, 5) < 0) {
printf("listen() error\n");
exit(1);
}
clnt_addr_size = sizeof(clnt_addr);
clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr,
&clnt_addr_size);
if (clnt_sock < 0) {
printf("accept() error\n");
exit(1);
}
char buf[1024];
printf("connected with ip: %s and port: %d\n",
inet_ntop(AF_INET, &clnt_addr.sin_addr, buf, 1024),
ntohs(clnt_addr.sin_port));
while (1) {
memset(buf, 0, sizeof(buf));
ssize_t size = read(clnt_sock, buf, sizeof(buf) - 1);
if (size < 0) {
printf("read() error\n");
break;
} else {
printf("client#: %s\n", buf);
}
}
close(clnt_sock);
close(serv_sock);
return 0;
}
客戶端代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
int clnt_sock;
struct sockaddr_in serv_addr;
int str_len;
if (argc != 3) {
printf("Usage: %s <IP> <port>\n", argv[0]);
exit(1);
}
clnt_sock = socket(AF_INET, SOCK_STREAM, 0);
if (clnt_sock < 0) {
printf("sock() error\n");
exit(1);
}
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(clnt_sock,
(struct sockaddr*)&serv_addr,
sizeof(serv_addr)) == -1) {
printf("connect() error\n");
exit(1);
}
char buf[1024];
while (1) {
memset(buf, 0, sizeof(buf));
printf("client enter: ");
fflush(stdout);
ssize_t size = read(0, buf, sizeof(buf) - 1);
if (size < 0) {
printf("read() error\n");
break;
} else {
buf[size - 1] = '\0';
}
write(clnt_sock, buf, strlen(buf));
}
close(clnt_sock);
return 0;
}
使用方法
編譯:
$ gcc hello_server.c -o hello_server
$ gcc hello_client.c -o hello_client
打開終端:
$ ./hello_server 8888
打開另一個(gè)終端:
$ ./hello_client 127.0.0.1 8888
運(yùn)行結(jié)果
客戶端:
$ ./hello_client 127.0.0.1 8888
client please enter: 123
client please enter: 456
client please enter:
服務(wù)端:
$ ./hello_server 8888
connected with ip: 127.0.0.1 and port: xxxx
client#: 123
client#: 456
- 我的個(gè)人主頁:http://www.techping.cn/
- 我的個(gè)人站點(diǎn)博客:http://www.techping.cn/blog/wordpress/
- 我的CSDN博客:http://blog.csdn.net/techping
- 我的簡(jiǎn)書:http://www.reibang.com/users/b2a36e431d5e/timeline
- 我的GitHub:https://github.com/techping
歡迎相互follow~