1 /* client_udp.c: */
2 #include <stdio.h>
3 #include <errno.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <unistd.h>
8 #include <string.h>
9
10 #define MAX_BUF_SIZE 1024
11 struct sockaddr_in srvaddr,clientaddr;
12 int sockfd,port,addrlen;
13 char s_msg[MAX_BUF_SIZE];
14 char receive[MAX_BUF_SIZE];
15 char bye[MAX_BUF_SIZE] = "bye\n";
16 void send_msg(void);//线程函数,发消息
17
18 int main(int argc, char *argv[])
19 {
20 pthread_t pt_send = 0;
21 int ret;
22 //bye="bye\n";
23 if (argc!=3) {
24 printf("Usage:%s server_ip server_port\n", argv[0]);
25 return -1;
26 }
27
28 //convert port number
29 if ((port=atoi(argv[2]))<0) {
30 printf("port error\n");
31 return -1;
32 }
33
34 //create client udp socket
35 sockfd=socket(AF_INET,SOCK_DGRAM,0);
36 if(sockfd==-1){
37 printf("Socket create error\n");
38 return -1;
39 }
40
41 //assign server's sock address
42 addrlen=sizeof(struct sockaddr_in);
43 memset(&srvaddr, 0, sizeof(srvaddr));
44 srvaddr.sin_family=AF_INET;
45 srvaddr.sin_port=htons(port);
46 //convert IP address
47 if(inet_aton(argv[1],&srvaddr.sin_addr)<0){
48 printf("IP error\n");
49 close(sockfd);
50 return -1;
51 }
52 printf("input bye to end CLIENT\n");
53 ret = pthread_create (&pt_send, NULL, (void *)send_msg, NULL);
54 if (ret != 0)
55 printf("pthread send_msg creat error\n");
56 while(1)
57 {
58 memset(receive,0, MAX_BUF_SIZE);
59 //receive message from remote server and print it
60 recvfrom(sockfd, receive, MAX_BUF_SIZE, MSG_DONTWAIT, (struct sockaddr *)(&clientaddr), &addrlen);
61 if (receive[0] != '\0' )
62 fprintf(stdout, "Receive message from server: %s", receive);
63 if (strcmp(receive,bye) == 0)
64 {
65 //printf("end session\n");
66 break;
67 }
68 }
69
70 //close socket
71 pthread_join (pt_send, NULL);
72 close(sockfd);
73 return 0;
74 }
75 void send_msg(void)
76 {
77 while(1)
78 {
79 memset(s_msg, 0, MAX_BUF_SIZE);
80
81 //get message from standard input
82 fgets(s_msg, MAX_BUF_SIZE, stdin);
83
84 //send message to remote server and print it
85 sendto(sockfd,s_msg,strlen(s_msg),MSG_DONTWAIT,(struct sockaddr *)(&srvaddr),addrlen);
86 printf("Client input message: %s", s_msg);
87 if (strcmp(s_msg, bye) == 0)
88 {
89 printf("end session\n");
90 break;
91 }
92 }
93 }
/* server_udp.c: */
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#define MAX_MSG_SIZE 1024
#define SERVER_PORT 3000
char s_msg[MAX_MSG_SIZE];//发消息
char receive[MAX_MSG_SIZE];//收消息
char bye[MAX_MSG_SIZE]="bye\n";
struct sockaddr_in srvaddr,clientaddr;
int sockfd,addrlen;
void send_msg(void);//线程函数,发消息
int main()
{
pthread_t pt_send = 0;
int ret;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)//套接字描述符
{
printf("Socket create error\n");
return -1;
}
//assign server's sock address
memset(&srvaddr, 0, sizeof(srvaddr));
addrlen=sizeof(srvaddr);
srvaddr.sin_family=AF_INET;
srvaddr.sin_port=htons(SERVER_PORT);
if(inet_aton("127.0.0.1",&srvaddr.sin_addr)==-1)
{
printf("addr convert error\n");
close(sockfd);
return -1;
}
//srvaddr.sin_addr.s_addr=htonl(INADDR_ANY);
//bind IP address and port
if(bind(sockfd,(struct sockaddr *)&srvaddr,addrlen)==-1)
{
printf("bind error\n");
close(sockfd);
return -1;
}
printf("input bye to end SERVER\n");
ret = pthread_create (&pt_send, NULL, (void *)send_msg, NULL);
if (ret != 0)
printf("pthread send_msg creat error\n");
while(1)
{
memset(receive, 0, MAX_MSG_SIZE);
recvfrom(sockfd, receive, MAX_MSG_SIZE, MSG_DONTWAIT, (struct sockaddr *)(&clientaddr), &addrlen);//非阻塞
if (receive[0] != '\0' )
fprintf(stdout, "Receive message from client: %s", receive);
if (strcmp(receive,bye) == 0)
break;
}
//while收消息,显示到标准输出,发消息,从标准如入写
//close socket
pthread_join (pt_send, NULL);
close(sockfd);
return 0;
}
void send_msg(void)
{
while(1)
{
memset(s_msg, 0, MAX_MSG_SIZE);
//get message from standard input
fgets(s_msg, MAX_MSG_SIZE, stdin);
//send message to client
sendto(sockfd,s_msg,strlen(s_msg),MSG_DONTWAIT,(struct sockaddr *)(&clientaddr),addrlen);
printf("Server input message: %s", s_msg);
if (strcmp(s_msg, bye) == 0)
{
printf("end session\n");
break;
}
}
}