您的位置:首页 > 理论基础 > 计算机网络

linux下TCP/IP实现简单聊天程序

2016-01-06 23:17 771 查看
可以在同一台电脑上运行,在一个终端上运行服务器端,在一个终端上运行客户端。

服务器端的IP地址要和本地的IP相同,并分配端口号,客户端的默认设置为本地,端口号自动分配。

服务器端:

?
客户端:

?
服务器端执行 : ./s 192.168.142.132 7575 5

客户端执行: ./c 192.168.142.132 7575

其中,192.168.142.132是本机的IP地址.

服务器端信息:

root@jieniyimiao-virtual-machine:/home/jieniyimiao/c_code/linux/ch13/sock_tcp_p_p_chat# ./s 192.168.142.132 7575 5

wait for connect

server: got connection from 192.168.142.132, port 44698, socket 4

input the message to send:jieniyimiap

input the message to send:ddddddddddddddddddddddddddd

input the message to send:dddddddddddddddddddddddddddd

input the message to send:aaaaaaaaaaaaaaaaaaaaaa

input the message to send:aaaaaaaaaaaaaaaaaaaaaaaa

input the message to send:message recv successful :'dddddddddddddddddd',18Byte recv

message recv successful :'ddddddddddddddddd',17Byte recv

message recv successful :'ddddddddddddddddd',17Byte recv

message recv successful :'dddddddddddddd',14Byte recv

message recv successful :'ddddddddddddddd',15Byte recv

message recv successful :'ddddddddddddddddd',17Byte recv

message recv successful :'dddddddddddddd',14Byte recv

quit

i will close the connect!

客户端略:

用NETSTAT查看信息如下:

# netstat |grep 192.168.142.132

**********************************************************************************

tcp 0 0 192.168.142.132:7575 192.168.142.132:44698 ****

tcp 0 0 192.168.142.132:44698 192.168.142.132:7575 ****

from: http://www.open-open.com/lib/view/open1432210304630.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: