linux下C语言socket网络编程简例

linux下C语言socket网络编程简例

1 已有 67 次阅读  2012-08-13 09:31  标签:  网络编程  linux  C语言 

这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到客户端的连接后,发送数据给客户端;客户端在接受到数据后打印出来,然后关闭。程序里有详细的说明,其中对具体的结构体和函数的实现可以参考其他资料。

程序说明: 这里服务器的端口号和ip地址使用固定的设置,移植时可以根据具体情况更改,可以改写为参数传递更好,这里为了方便,使用固定的。

移植时服务端可以不用更改,编译后可直接运行;客户端将ip改为服务器的地址,然后编译运行。可以使用netstat 进行查看相应的运行状态。

01 /*************************************
02 文件名: server.c
03 linux 下socket网络编程简例  - 服务端程序
04 服务器端口设为 0x8888   (端口和地址可根据实际情况更改,或者使用参数传入)
05 服务器地址设为 192.168.1.104
06 作者:kikilizhm#163.com (将#换为@)
07 */
08  
09 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <stdio.h>
12 #include <sys/socket.h>
13 #include <linux/in.h>
14 #include <string.h>
15  
16 int main()
17 {
18 int sfp,nfp; /* 定义两个描述符 */
19 struct sockaddr_in s_add,c_add;
20 int sin_size;
21 unsigned short portnum=0x8888; /* 服务端使用端口 */
22  
23 printf("Hello,welcome to my server !\r\n");
24 sfp = socket(AF_INET, SOCK_STREAM, 0);
25 if(-1 == sfp)
26 {
27     printf("socket fail ! \r\n");
28     return -1;
29 }
30 printf("socket ok !\r\n");
31  
32 /* 填充服务器端口地址信息,以便下面使用此地址和端口监听 */
33 bzero(&s_add,sizeof(struct sockaddr_in));
34 s_add.sin_family=AF_INET;
35 s_add.sin_addr.s_addr=htonl(INADDR_ANY); /* 这里地址使用全0,即所有 */
36 s_add.sin_port=htons(portnum);
37 /* 使用bind进行绑定端口 */
38 if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
39 {
40     printf("bind fail !\r\n");
41     return -1;
42 }
43 printf("bind ok !\r\n");
44 /* 开始监听相应的端口 */
45 if(-1 == listen(sfp,5))
46 {
47     printf("listen fail !\r\n");
48     return -1;
49 }
50 printf("listen ok\r\n");
51  
52 while(1)
53 {
54 sin_size = sizeof(struct sockaddr_in);
55 /* accept服务端使用函数,调用时即进入阻塞状态,等待用户进行连接,在没有客户端进行连接时,程序停止在此处,
56    不会看到后面的打印,当有客户端进行连接时,程序马上执行一次,然后再次循环到此处继续等待。
57    此处accept的第二个参数用于获取客户端的端口和地址信息。
58     */
59 nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
60 if(-1 == nfp)
61 {
62     printf("accept fail !\r\n");
63     return -1;
64 }
65 printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n",ntohl(c_add.sin_addr.s_addr),ntohs(c_add.sin_port));
66  
67 /* 这里使用write向客户端发送信息,也可以尝试使用其他函数实现 */
68 if(-1 == write(nfp,"hello,welcome to my server \r\n",32))
69 {
70     printf("write fail!\r\n");
71     return -1;
72 }
73 printf("write ok!\r\n");
74 close(nfp);
75  
76 }
77 close(sfp);
78 return 0;
79 }
01 /*************************************
02 文件名: client.c
03 linux 下socket网络编程简例  - 客户端程序
04 服务器端口设为 0x8888   (端口和地址可根据实际情况更改,或者使用参数传入)
05 服务器地址设为 192.168.1.104
06 作者:kikilizhm#163.com (将#换为@)
07 */
08  
09 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <stdio.h>
12 #include <sys/socket.h>
13 #include <linux/in.h>
14 #include <string.h>
15  
16 int main()
17 {
18 int cfd; /* 文件描述符 */
19 int recbytes;
20 int sin_size;
21 char buffer[1024]={0};    /* 接受缓冲区 */
22 struct sockaddr_in s_add,c_add; /* 存储服务端和本端的ip、端口等信息结构体 */
23 unsigned short portnum=0x8888;  /* 服务端使用的通信端口,可以更改,需和服务端相同 */
24  
25 printf("Hello,welcome to client !\r\n");
26 /* 建立socket 使用因特网,TCP流传输 */
27 cfd = socket(AF_INET, SOCK_STREAM, 0);
28 if(-1 == cfd)
29 {
30     printf("socket fail ! \r\n");
31     return -1;
32 }
33 printf("socket ok !\r\n");
34 /* 构造服务器端的ip和端口信息,具体结构体可以查资料 */
35 bzero(&s_add,sizeof(struct sockaddr_in));
36 s_add.sin_family=AF_INET;
37 s_add.sin_addr.s_addr= inet_addr("192.168.1.104"); /* ip转换为4字节整形,使用时需要根据服务端ip进行更改 */
38 s_add.sin_port=htons(portnum); /* 这里htons是将short型数据字节序由主机型转换为网络型,其实就是
39     将2字节数据的前后两个字节倒换,和对应的ntohs效果、实质相同,只不过名字不同。htonl和ntohl是
40     操作的4字节整形。将0x12345678变为0x78563412,名字不同,内容两两相同,一般情况下网络为大端,
41     PPC的cpu为大端,x86的cpu为小端,arm的可以配置大小端,需要保证接收时字节序正确。
42  */
43  
44 printf("s_addr = %#x ,port : %#x\r\n",s_add.sin_addr.s_addr,s_add.sin_port); /* 这里打印出的是小端
45     和我们平时看到的是相反的。 */
46  
47 /* 客户端连接服务器,参数依次为socket文件描述符,地址信息,地址结构大小 */
48 if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
49 {
50     printf("connect fail !\r\n");
51     return -1;
52 }
53 printf("connect ok !\r\n");
54 /*连接成功,从服务端接收字符*/
55 if(-1 == (recbytes = read(cfd,buffer,1024)))
56 {
57     printf("read data fail !\r\n");
58     return -1;
59 }
60 printf("read ok\r\nREC:\r\n");
61  
62 buffer[recbytes]='\0';
63 printf("%s\r\n",buffer);
64  
65 getchar(); /* 此句为使程序暂停在此处,可以使用netstat查看当前的连接 */
66 close(cfd); /* 关闭连接,本次通信完成 */
67 return 0;
68  
69  
70  
71 }
运行截图:
linux下C语言socket网络编程简例_第1张图片
转自:http://blog.csdn.net/kikilizhm/article/details/7858405

你可能感兴趣的:(linux下C语言socket网络编程简例)