【MYD-JX8MMA7】(三)网络通讯
1.TCP-Client
/* Copyright 2019 Tronlong Elec. Tech. Co. Ltd. All Rights Reserved. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
// check the arguments.
if (argc != 3) {
printf("Usage:\n");
printf(" %s \n", argv[0]);
return -1;
}
char *addr = argv[1];
short port = atoi(argv[2]);
int sock_fd;
int ret;
char recv_buf[BUFFER_SIZE], send_buf[BUFFER_SIZE];
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(addr);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) {
perror("socket");
return -1;
}
ret = connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
if (ret == -1) {
perror("connect");
goto err;
}
printf("connect server(IP:%s Port: %d).\\n", addr, port);
pid_t pid = fork();
if (pid > 0) {
while (1) {
bzero(send_buf, sizeof(send_buf));
char *str = fgets(send_buf, sizeof(send_buf), stdin);
if (str == NULL) {
perror("fgets");
goto err;
}
if (send(sock_fd, send_buf, strlen(send_buf) - 1, 0) < 0) {
perror("send");
goto err;
}
}
}
else if (pid == 0) {
while (1) {
bzero(recv_buf, sizeof(recv_buf));
if (sock_fd > 0) {
if ((recv(sock_fd, recv_buf, BUFFER_SIZE, 0)) <= 0) {
perror("recv");
goto err;
}
printf("[Recv from server %s:%d] : %s\\n",
inet_ntoa(server_addr.sin_addr),
htons(server_addr.sin_port),
recv_buf);
}
}
}
else {
perror("fork");
goto err;
}
close(sock_fd);
return 0;
err:
close(sock_fd);
return -1;
}
2.TCP-Server
/* Copyright 2019 Tronlong Elec. Tech. Co. Ltd. All Rights Reserved. */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ipc.h>
#include <errno.h>
#include <sys/shm.h>
#include <time.h>
#include <pthread.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 1024 // buf size
#define LISTENQ 1 // the max quote
#define SIZE_SHMADD 2048
int main(int argc, char *argv[]) {
// check the arguments.
if (argc != 2) {
printf("Usage:\n");
printf(" %s \n", argv[0]);
return -1;
}
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
char recv_buf[BUFFER_SIZE];
char send_buf[BUFFER_SIZE];
pid_t pid;
int sock_fd, conn_fd = -1;
int optval = 1;
int port = atoi(argv[1]);
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
return -1;
}
if ((setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR,
(void *)&optval, sizeof(int))) < 0) {
perror("setsockopt");
goto err;
}
bzero(&server_addr, sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port = htons(port);
if (bind(sock_fd, (struct sockaddr *)(&server_addr),
sizeof(struct sockaddr)) == -1) {
perror("bind");
goto err;
}
if (listen(sock_fd, LISTENQ) == -1) {
perror("listen");
goto err;
}
printf("Waiting for client connectionn...\\n");
socklen_t cli_len = sizeof(struct sockaddr_in);
conn_fd = accept(sock_fd, (struct sockaddr *)(&client_addr), &cli_len);
if (conn_fd == -1) {
perror("accept");
close(sock_fd);
return -1;
}
printf("Client accepted : %s:%d \\n",
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
pid = fork();
if (pid > 0) {
while (1) {
bzero(send_buf, sizeof(send_buf));
char *str = fgets(send_buf, sizeof(send_buf), stdin);
if (str == NULL) {
perror("fgets");
goto err;
}
if (send(conn_fd, send_buf, strlen(send_buf) - 1, 0) < 0) {
perror("send");
goto err;
}
}
}
else if (pid == 0) {
while (1) {
bzero(recv_buf, sizeof(recv_buf));
if ((recv(conn_fd, recv_buf, BUFFER_SIZE, 0)) <= 0) {
perror("recv");
goto err;
}
printf("[Recv from client %s:%d] : %s\\n",
inet_ntoa(server_addr.sin_addr),
htons(server_addr.sin_port),
recv_buf);
}
}
else {
perror("fork");
goto err;
}
close(conn_fd);
close(sock_fd);
return 0;
err:
close(conn_fd);
close(sock_fd);
return -1;
}
3.UDP-Client
/* Copyright 2019 Tronlong Elec. Tech. Co. Ltd. All Rights Reserved. */
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 1024 // buf size
int main(int argc, char *argv[]) {
// check the arguments.
if (argc != 3) {
printf("Usage:\n");
printf(" %s <server_addr> \n", argv[0]);
return -1;
}
int sock_fd;
char send_buf[BUFFER_SIZE];
char recv_buf[BUFFER_SIZE];
struct sockaddr_in server_addr;
char *addr = argv[1];
short port = atoi(argv[2]);
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(addr);
server_addr.sin_port = htons(port);
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd == -1) {
perror("socket");
return -1;
}
pid_t pid = fork();
if (pid > 0) {
while (1) {
bzero(send_buf, sizeof(send_buf));
char *str = fgets(send_buf, sizeof(send_buf), stdin);
if (str == NULL) {
perror("fgets");
goto err;
}
if (strlen(str) <= 1) {
continue;
}
if ((sendto(sock_fd, send_buf, strlen(str) - 1, 0,
(struct sockaddr *)&server_addr,
sizeof(struct sockaddr_in))) < 0) {
perror("sendto");
goto err;
}
}
}
else if (pid == 0) {
while (1) {
bzero(recv_buf, sizeof(recv_buf));
socklen_t addr_len = sizeof(struct sockaddr_in);
if ((recvfrom(sock_fd, recv_buf, sizeof(recv_buf), 0,
(struct sockaddr *)&server_addr,
&addr_len)) <= 0) {
perror("recvfrom");
goto err;
}
printf("[Recv from server %s:%d] : %s\\n",
inet_ntoa(server_addr.sin_addr),
htons(server_addr.sin_port),
recv_buf);
}
}
close(sock_fd);
return 0;
err:
close(sock_fd);
return -1;
}
4.UDP-Server
/* Copyright 2019 Tronlong Elec. Tech. Co. Ltd. All Rights Reserved. */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <pthread.h>
#define BUFFER_SIZE 1024 // buf size
struct CONNECT_INFO {
struct sockaddr_in client_addr;
};
static int sock_fd;
static volatile bool is_connect = false;
bool udp_server_init(short port) {
struct sockaddr_in server_addr;
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd == -1) {
perror("socket");
return false;
}
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if ((bind(sock_fd, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr_in))) == -1) {
perror("bind");
return false;
}
return true;
}
void *send_thread(void *arg) {
struct CONNECT_INFO *info = (struct CONNECT_INFO *)arg;
if (info == NULL) {
printf("error connect info");
return (void* )-1;
}
while (1) {
char send_buf[BUFFER_SIZE];
bzero(send_buf, sizeof(send_buf));
char *str = fgets(send_buf, sizeof(send_buf), stdin);
if (str == NULL) {
perror("fgets");
continue;
}
if (strlen(str) <= 1) {
continue;
}
if(is_connect == false) {
printf("No client are available!\\n");
continue;
}
if ((sendto(sock_fd, send_buf, strlen(str) - 1, 0,
(struct sockaddr *)&info->client_addr,
sizeof(struct sockaddr_in))) < 0) {
perror("sendto");
continue;
}
}
return (void*)0;
}
int main(int argc, char *argv[]) {
// check your input.
if (argc != 2) {
printf("Usage:\n");
printf(" %s \n", argv[0]);
return -1;
}
short port = atoi(argv[1]);
bool ret = udp_server_init(port);
if (ret == false) {
goto err;
}
pthread_t t_id;
struct CONNECT_INFO info;
socklen_t addr_len = sizeof(struct sockaddr_in);
memset(&info, 0, sizeof(info));
pthread_create(&t_id, NULL, send_thread, (void *)&info);
pthread_detach(t_id);
printf("Waiting for client connectionn\\n");
while (1) {
char recv_buf[BUFFER_SIZE];
bzero(recv_buf, sizeof(recv_buf));
if ((recvfrom(sock_fd, recv_buf, sizeof(recv_buf), 0,
(struct sockaddr *)&info.client_addr, &addr_len)) <= 0) {
perror("recvfrom");
goto err;
}
printf("[Recv from client %s:%d] : %s\\n",
inet_ntoa(info.client_addr.sin_addr),
htons(info.client_addr.sin_port),
recv_buf);
is_connect = true;
}
close(sock_fd);
return 0;
err:
close(sock_fd);
return -1;
}
5.makefile文件
VPATH =
LIB_DIR =
INCLUDES =
CFLAGS = -Wall -g
LIBS = -lpthread
OBJS =
TARGET = tl_tcp_client tl_tcp_server tl_udp_client tl_udp_server
all:$(TARGET)
tl_tcp_client:tl_tcp_client.o $(OBJS)
(CC) -o @ **^ **(INCLUDES) $(LIBS)
tl_tcp_server:tl_tcp_server.o $(OBJS)
(CC) -o @ **^ **(INCLUDES) $(LIBS)
tl_udp_client:tl_udp_client.o $(OBJS)
(CC) -o @ **^ **(INCLUDES) $(LIBS)
tl_udp_server:tl_udp_server.o $(OBJS)
(CC) -o @ **^ **(INCLUDES) $(LIBS)
%.o:%.c
(CC) -c < -o **@ **(INCLUDES) $(CFLAGS)
%.d:%.c
[url=home.php?mod=space&uid=3170703]@Set[/url] -e; **(CC) -MM **< **(INCLUDES) > **@.$$
;
sed 's,(**\)\.o[ :],\1.o **@ : ,g' < $@.
$@;
rm -f $@.
%.o:%.cpp
(CC) -c < -o **@ **(INCLUDES) $(CFLAGS)
%.d:%.cpp
@set -e; **(CC) -MM **< **(INCLUDES) > **@.$$
;
sed 's,(**\)\.o[ :],\1.o **@ : ,g' < $@.
$@;
rm -f $@.
sinclude $(OBJS:.o=.d)
.PHONY:
clean:
rm -f $(TARGET) *.o *.d .d.
install:$(TARGET)
cp **(TARGET) **(INSTALL_DIR)
6.代码编译
7.程序运行