Socket套接字编程(一)——TCP编程

来源:互联网 发布:淘宝索尼手机 编辑:程序博客网 时间:2024/06/09 17:45

开始研究网络编程,先从套接字编程开始,这个简单,不过也得会才行。随手写点代码先练练手,没多少技术含量。

先熟悉下流程:

TCP型套接字:

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}p{mso-margin-top-alt:auto;margin-right:0cm;mso-margin-bottom-alt:auto;margin-left:0cm;mso-pagination:widow-orphan;font-size:12.0pt;font-family:宋体;mso-bidi-font-family:宋体;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:36.0pt;mso-footer-margin:36.0pt;mso-paper-source:0;}div.Section1{page:Section1;}-->

服务器端程序:
1
、加载套接字库
2
、创建套接字(socket)。
3
、将套接字绑定到一个本地地址和端口上(bind)。
4
、将套接字设为监听模式,准备接收客户请求(listen)。
5
、等待客户请求到来;当请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept)。
6
、用返回的套接字和客户端进行通信(send/recv)。
7
、返回,等待另一客户请求。
8
、关闭套接字。
客户端程序:
1
、加载套接字库
2
、创建套接字(socket)。
3
、向服务器发出连接请求(connect)。
4
、和服务器端进行通信(send/recv)。
5
、关闭套接字。

 

下面直接上代码:

服务器端代码:

     客户端代码:

    

 

测试了一下没多大问题,但也存在很多问题,什么问题呢?下回分解

原创粉丝点击