思维导图
tcp_server代码
#include<myhead.h>
#define PORT 8888 //端口号
#define IP "192.168.114.240" //IP地址
int main(int argc, const char *argv[])
{
// 创建套接字socket
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
ERR_MSG("socket");
return -1;
}
//允许端口快速的被复用
int reuse = 1;
if(setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速的被复用成功\n");
// 设置服务器地址
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(IP); // 使用任意可用的IP地址
server_addr.sin_port = htons(PORT); // 服务器监听的端口
// 绑定地址 bind函数
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
ERR_MSG("bind");
return 1;
}
// 开始监听连接 listen函数
if (listen(server_socket, 10) == -1) {
ERR_MSG("listen");
return 1;
}
printf("Server listening on port PORT...\n");
// 接受连接 获取一个已经完成的客户端信息,生成一个新的文件描述符accepct
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_socket == -1) {
ERR_MSG("accept");
return 1;
}
printf("[%s:%d]客户端连接 client_addr = %d\n",\
inet_ntoa(client_addr.sin_addr),\
ntohs(client_addr.sin_port), client_socket);
// 在此处可以进行通信处理
char buffer[1024]; //定义容器
ssize_t bytes_received;
while (1) {
//接受数据
bzero(buffer, sizeof(buffer));
bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received < 0) {
ERR_MSG("recv");
break;
}else if(0 == bytes_received){
printf("[%s:%d]客户端下线 client_addr = %d\n",\
inet_ntoa(client_addr.sin_addr),\
ntohs(client_addr.sin_port), client_socket);
break;
}
printf("[%s:%d]客户端连接 client_addr = %d\n",\
inet_ntoa(client_addr.sin_addr),\
ntohs(client_addr.sin_port), client_socket);
//buffer[bytes_received] = '\0'; // 将数据末尾设置为空字符
printf("Received: %s\n", buffer);
//发送
char message[1024] = "";
scanf("%s", message);
ssize_t bytes_sent = send(client_socket, message, strlen(message), 0);
if (bytes_sent == -1) {
ERR_MSG("send");
break;
}
}
// 关闭客户端套接字
if (close(client_socket) < 0) {
ERR_MSG("close");
return -1;
}
// 关闭套接字
if(close(server_socket < 0))
{
ERR_MSG("close");
return -1;
}
return 0;
}
tcp_client代码
#include<myhead.h>
#define IP "192.168.114.240" // 服务器IP地址
#define PORT 8888 // 服务器端口号
int main() {
// 创建套接字 socket
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("socket");
return 1;
}
// 设置服务器地址
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(IP);
server_addr.sin_port = htons(PORT);
// 连接服务器 connect 函数
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
close(client_socket);
return 1;
}
printf("Connected to server at %s:%d\n", IP, PORT);
char message[1024]; //发送数据容器
char buffer[1024]; //接收数据容器
while(1)
{
//清空字符串
bzero(buffer, sizeof(buffer));
printf("请输入:");
fgets(buffer, sizeof(buffer), stdin);
buffer[strlen(buffer)-1] = 0;
bzero(buffer, sizeof(buffer));
// 发送数据
scanf("%s", message);
ssize_t bytes_send = send(client_socket, message, strlen(message), 0);
if (bytes_send == -1) {
ERR_MSG("send");
return 1;
}
// 接收数据
bzero(buffer, sizeof(buffer));
ssize_t bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received <= 0) {
ERR_MSG("recv");
return 1;
}
printf("Received from server: %s\n", buffer);
}
// 关闭套接字
if (close(client_socket) == -1) {
ERR_MSG("close");
return 1;
}
return 0;
}
udp_client代码
#include<myhead.h>
#define PORT 8888
#define IP "192.168.114.240"
int main(int argc, const char *argv[])
{
//新建套接字文件
int client_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(client_socket == -1){
ERR_MSG("socket");
return -1;
}
printf("socket success\n");
//定义要发送的地址信息结构体
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(IP);
char msg[128] = "hello world";
char buf[128] = "";
ssize_t ret;
while(1){
//发送数据
sleep(2);
sendto(client_socket, msg, sizeof(msg), 0,\
(const struct sockaddr *)&server_addr,\
sizeof(server_addr));
printf("已将%s发送给[%s:%d] client_socket = %d\n",msg,\
inet_ntoa(server_addr.sin_addr),\
ntohs(server_addr.sin_port), client_socket);
//接收数据
//清空字符串
bzero(buf, sizeof(buf));
socklen_t server_addr_len = sizeof(server_addr);
ret = recv(client_socket, buf, sizeof(buf), 0);
if(ret == -1){
ERR_MSG("recvfrom");
break;
}
printf("已将%s从[%s:%d]接收 client_socket = %d\n",buf,\
inet_ntoa(server_addr.sin_addr),\
ntohs(server_addr.sin_port), client_socket);
}
//关闭套接字文件
if(close(client_socket) == -1){
ERR_MSG("close");
return -1;
}
return 0;
}
运行截图