C语言 linux环境基于socket的简易即时通信程序

转载请注明出处:http://www.cnblogs.com/kevince/p/3891033.html      ——By Kevince

最近在看linux网络编程相关,现学现卖,就写了一个简易的C/S即时通信程序,代码如下:

head.h

 1 /*头文件,client和server编译时都需要使用*/

 2 #include <unistd.h>

 3 #include <stdio.h>

 4 #include <sys/types.h>

 5 #include <sys/socket.h>

 6 #include <string.h>

 7 #include <pthread.h>

 8 #include <stdlib.h>

 9 #include <netinet/in.h>

10 #include <arpa/inet.h>

11 

12 #define MSGLEN 1000

13 #define IPLEN 15

14 

15 typedef int SOCKET;

server.c:

 1 /*server*/

 2 

 3 #include "head.h"

 4 

 5 char msg_recv[MSGLEN], msg_send[MSGLEN];

 6 SOCKET server_sockfd, client_sockfd;

 7 

 8 void *thread_function(void *argv) /*线程函数*/

 9 {

10     while(1){

11         gets(msg_send);

12         write(client_sockfd, msg_send, MSGLEN);

13     }

14     pthread_exit(NULL);

15 }

16 

17 int main(int arg, char *argv[])

18 {

19     int server_len, client_len;

20     struct sockaddr_in server_address;

21     struct sockaddr_in client_address;

22     int port;

23     int res;

24 

25     pthread_t a_thread;

26     void *thread_result;

27 

28     if (arg != 2){

29         printf("server --portnum\n");

30         exit(EXIT_FAILURE);

31     }

32 

33     sscanf(argv[1], "%d", &port); /*读入端口*/

34 

35     server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

36     server_address.sin_family = AF_INET;

37     server_address.sin_addr.s_addr = inet_addr("127.0.0.1");

38     server_address.sin_port = htons(port);

39 

40     server_len = sizeof(server_address);

41     bind(server_sockfd, (struct sockaddr *)&server_address, server_len); /*绑定端口并监听*/

42     listen(server_sockfd, 10);

43     printf("listen...\n");

44 

45     client_len = sizeof(client_address);

46     client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);

47     printf("connection success!\n");

48 

49     res = pthread_create(&a_thread, NULL, thread_function, NULL); /*启动线程函数*/

50     if (res != 0){

51         perror("Thread creation failed");

52         exit(EXIT_FAILURE);

53     }

54 

55     while(read(client_sockfd, msg_recv, MSGLEN)){

56         printf("msg from client: %s\n", msg_recv);

57     }

58     close(client_sockfd);

59     exit(EXIT_SUCCESS);

60 }

 

client.c:

 1 /*client*/

 2 

 3 #include "head.h"

 4 

 5 char msg_recv[MSGLEN],msg_send[MSGLEN];

 6 SOCKET sockfd;

 7 

 8 void *thread_function(void *argv) /*线程函数*/

 9 {

10     while(1){

11         gets(msg_send);

12         write(sockfd, msg_send, MSGLEN);

13     }

14     pthread_exit(NULL);

15 }

16 

17 int main(int arg, char *argv[])

18 {

19     struct sockaddr_in address;

20     int len;

21     int res;

22     int port;

23     char ip[IPLEN];

24 

25     void *thread_result;

26     pthread_t a_thread;

27 

28     sockfd = socket(AF_INET, SOCK_STREAM, 0);

29 

30     if (arg != 3){

31         printf("client --ipaddress --portnum\n");

32         exit(EXIT_FAILURE);

33     }

34 

35     sscanf(argv[1], "%s", ip);

36     sscanf(argv[2], "%d", &port); /*读取ip与端口*/

37 

38     address.sin_family = AF_INET;

39     address.sin_addr.s_addr = inet_addr(ip);

40     address.sin_port = htons(port);

41 

42     len = sizeof(address);

43     res = connect(sockfd, (struct sockaddr *)&address, len);

44     if (res == -1){

45         perror("connect failed! ");

46         exit(EXIT_FAILURE);

47     }

48     printf("connection success!\n");

49 

50     res = pthread_create(&a_thread, NULL, thread_function, NULL); /*启动线程函数*/

51     if (res != 0){

52         perror("Thread creation failed");

53         exit(EXIT_FAILURE);

54     }

55 

56     while(read(sockfd, msg_recv, MSGLEN)){

57         printf("msg from server: %s\n", msg_recv);

58     }

59     res = pthread_join(a_thread, &thread_result);

60     if (res != 0){

61         perror("joined failed");

62         exit(EXIT_FAILURE);

63     }

64     exit(EXIT_SUCCESS);

65 }

 

 

由于使用了线程,所以要链接正确的线程库,所以编译命令如下:

gcc -D_REENTRANT -I/usr/include/nptl server.c -o server -L/usr/lib/nptl -lpthread



gcc -D_REENTRANT -I/usr/include/nptl client.c -o client -L/usr/lib/nptl -lpthread

 

如果你的系统默认使用的就是NPTL线程库,那么编译时就无需加上-I和-L选项

 

运行时输入的命令规则是:

./server --portnum  #即server后面要加上需要绑定的端口号。



./client --ip --portnum  #即client后面要加上服务器的IP地址以及端口号。

 

 

不积跬步无以至千里,虽然这两个程序很简单,但作为我的第一个linux环境下基于socket的通信程序,也很有纪念意义。

你可能感兴趣的:(socket)