linux網路編程示例 簡單TCP、UDP編程示例。 TCP TCP客戶端與伺服器通信模型 伺服器代碼 #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #include ...
linux網路編程示例
簡單TCP、UDP編程示例。
TCP
- TCP客戶端與伺服器通信模型
- 伺服器代碼
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
const char ip[] = "127.0.0.1";
const int port = 9006;
//創建socket,使用IP協議(PF_INET)+TCP協議(SOCK_STREAM)
int fd_listen = socket(PF_INET, SOCK_STREAM, 0);
//綁定固定ip:port地址
in_addr_t ip_num = inet_addr(ip);
sockaddr_in addr_server = {AF_INET, port, ip_num};
bind(fd_listen, (sockaddr *)&addr_server, sizeof(addr_server));
//監聽socket
listen(fd_listen, 10);
//接受客戶端連接,並返回連接socket
sockaddr_in addr_client;
socklen_t len_client_addr = sizeof(addr_client);
int fd_conn = accept(fd_listen, (sockaddr *)&addr_client, &len_client_addr);
//接收客戶端數據
printf("start receiving data...\n");
char buf[4096];
while (true)
{
memset(buf, '\0', sizeof(buf));
int ret = recv(fd_conn, buf, sizeof(buf) - 1, 0);
// recv返回0,表示客戶端斷開連接
if (ret == 0)
{
printf("connection closed\n");
break;
}
//列印接收的數據
printf("%s", buf);
}
//關閉socket
close(fd_conn);
close(fd_listen);
return 0;
}
- 客戶端代碼
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
const char ip[] = "127.0.0.1"; //伺服器ip
const int port = 9006; //伺服器port
//創建socket,使用IP協議(PF_INET)+TCP協議(SOCK_STREAM)
int fd_conn = socket(PF_INET, SOCK_STREAM, 0);
//連接伺服器
in_addr_t ip_num = inet_addr(ip);
sockaddr_in addr_server = {AF_INET, port, ip_num}; //伺服器地址
connect(fd_conn, (sockaddr *)&addr_server, sizeof(addr_server));
const char message[] = "hello world!\n";
for (int i = 0; i < 10; i++)
{
send(fd_conn, message, strlen(message), 0);
sleep(1);
}
//關閉socket
close(fd_conn);
return 0;
};
UDP
- UDP客戶端與伺服器通信模型
- 伺服器代碼
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
const char ip[] = "127.0.0.1";
const int port = 9006;
//創建socket,使用IP協議(PF_INET)+SOCK_DGRAM
int fd_socket = socket(PF_INET, SOCK_DGRAM, 0);
//綁定固定ip:port地址
in_addr_t ip_num = inet_addr(ip);
sockaddr_in addr_server = {AF_INET, port, ip_num};
bind(fd_socket, (sockaddr *)&addr_server, sizeof(addr_server));
//接收客戶端數據
printf("start receiving data...\n");
//存放客戶端地址
sockaddr_in addr_client;
socklen_t len_addr_client = sizeof(addr_client);
char buf[4096];
while (true)
{
//接收數據
memset(buf, '\0', sizeof(buf));
int ret = recvfrom(fd_socket, buf, sizeof(buf) - 1, 0, (sockaddr *)&addr_client, &len_addr_client);
char *ip_client = inet_ntoa(addr_client.sin_addr);
int port_client = addr_client.sin_port;
//列印接收的數據
printf("recived from %s:%d: %s\n", ip_client, port_client, buf);
}
//關閉socket
close(fd_socket);
return 0;
}
- 客戶端代碼
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
const char ip[] = "127.0.0.1";
const int port = 9006;
//創建socket,使用IP協議(PF_INET)+SOCK_DGRAM
int fd_socket = socket(PF_INET, SOCK_DGRAM, 0);
//綁定固定ip:port地址
in_addr_t ip_num = inet_addr(ip);
sockaddr_in addr_server = {AF_INET, port, ip_num};
bind(fd_socket, (sockaddr *)&addr_server, sizeof(addr_server));
//接收客戶端數據
printf("start receiving data...\n");
//存放客戶端地址
sockaddr_in addr_client;
socklen_t len_addr_client = sizeof(addr_client);
char buf[4096];
while (true)
{
//接收數據
memset(buf, '\0', sizeof(buf));
int ret = recvfrom(fd_socket, buf, sizeof(buf) - 1, 0, (sockaddr *)&addr_client, &len_addr_client);
char *ip_client = inet_ntoa(addr_client.sin_addr);
int port_client = addr_client.sin_port;
//列印接收的數據
printf("recived from %s:%d: %s\n", ip_client, port_client, buf);
}
//關閉socket
close(fd_socket);
return 0;
}