[Network Programming] Detailed explanation of socket

Reprinted: http://www.qqread.com/java/w592395600.html

    Socket is one end of two-way communication between two programs running on the network. It can accept requests and send requests. It can be used to write data transmission on the network more conveniently. In Java , there is a special Socket class to handle user requests and responses. Using the methods of the Socket class, the communication between the two computers can be realized. Here is how to use Socket for network programming in Java

  In Java , Socket can be understood as a special object on the client or server side. This object has two key methods, one is the getInputStream method and the other is the getOutputStream method. The getInputStream method can obtain an input stream. The input stream obtained by the getInputStream method on the client's Socket object is actually the data stream sent back from the server. The GetOutputStream method obtains an output stream, and the output stream returned by the getOutputStream method on the client Socket object is the data stream to be sent to the server (actually a buffer that temporarily stores the data to be sent in the past).

  Programs can further encapsulate these data streams as needed. The examples in this article encapsulate these data streams to a certain extent (for encapsulation, please refer to the implementation of streams in Java).

  In order to better illustrate the problem, here is an example of an online conversation. After the client is started, the server will start a thread to communicate with the client.

  To complete this work, three parts need to be completed, which are explained in turn:

  1. Establishing a server class

  There is a class in Java that is specially used to establish a Socket server, named ServerSocket, which can use the port number that the server needs to use as a parameter to Create a server object.

    The statement ServerSocket server = new ServerSocket(9998) 

  creates a server object that uses port 9998. When a client program establishes a Socket connection and the connected port number is 9998, the server object server responds to the connection, and the server.accept() method creates a Socket object. The server can use this Socket object to communicate with the client.

    Socket incoming = server.accept() 

  and then get the input stream and output stream, and encapsulate

    BufferedReader in = new BufferedReader(new 
                                                InputStreamReader(incoming.getInputStream()));
    PrintWriter out = new PrintWriter(incoming.getOutputStream(),true) ; 

  Then, you can use the in.readLine() method to get the client's input, or you can use the out.println() method to send data to the client. Therefore, it can respond to different requests of the client according to the needs of the program.

  These two data streams should be closed after all communications are over. The order of closing is to close the output stream first, and then close the input stream, that is, use 

out.close();
in.close(); 

Second, build client code

  Compared with the server side, the client side is simpler. The client side only needs to create a Socket object with the ip of the machine where the server is located and the port of the server as parameters. After getting this object, you can use the methods described in the "Building the Server" section to implement data input and output.

Socket socket = new Socket("168.160.12.42",9998);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);  the

  above And an input stream and an output stream are established, corresponding to the output of the server and the write of the client, respectively.



Reprinted: http://www.cnblogs.com/skynet/

"Everything is Socket!"

Although it is a little exaggerated, the fact is that almost all network programming now uses sockets .

——I feel interested in actual programming and open source project research.

        We are well versed in the value of information exchange . How do processes in the network communicate ? For example, when we open the browser to browse the web every day, how does the process of the browser communicate with the web server? When you use QQ to chat, how does the QQ process communicate with the server or the QQ process where your friends are located? These all have to rely on socket? So what is socket? What are the types of sockets? There are also the basic functions of socket, which are what this article wants to introduce. The main contents of this article are as follows:

  • 1. How to communicate between processes in the network?
  • 2. What is Socket?
  • 3. Basic operation of socket
    • 3.1, socket() function
    • 3.2, bind() function
    • 3.3、listen()、connect()函数
    • 3.4、accept()函数
    • 3.5、read()、write()函数等
    • 3.6、close()函数
  • 4、socket中TCP的三次握手建立连接详解
  • 5、socket中TCP的四次握手释放连接详解
  • 6、一个例子(实践一下)
  • 7、留下一个问题,欢迎大家回帖回答!!!

1、网络中进程之间如何通信?

本地的进程间通信(IPC)有很多种方式,但可以总结为下面4类:

  • 消息传递(管道、FIFO、消息队列)
  • 同步(互斥量、条件变量、读写锁、文件和写记录锁、信号量)
  • 共享内存(匿名的和具名的)
  • 远程过程调用(Solaris门和Sun RPC)

但这些都不是本文的主题!我们要讨论的是网络中进程之间如何通信?首要解决的问题是如何唯一标识一个进程,否则通信无从谈起!在本地可以通过进程PID来唯一标识一个进程,但是在网络中这是行不通的。其实TCP/IP协议族已经帮我们解决了这个问题,网络层的“ip地址”可以唯一标识网络中的主机,而传输层的“协议+端口可以唯一标识主机中的应用程序(进程)。这样利用三元组(ip地址,协议,端口)就可以标识网络的进程了,网络中的进程通信就可以利用这个标志与其它进程进行交互。

使用TCP/IP协议的应用程序通常采用应用编程接口UNIX  BSD的套接字(socket)和UNIX System V的TLI(已经被淘汰),来实现网络进程之间的通信。就目前而言,几乎所有的应用程序都是采用socket,而现在又是网络时代,网络中进程通信是无处不在,这就是我为什么说“一切皆socket”。

2、什么是Socket?

上面我们已经知道网络中的进程是通过socket来通信的,那什么是socket呢?socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模式来操作。我的理解就是Socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭),这些函数我们在后面进行介绍。

socket一词的起源

在组网领域的首次使用是在1970年2月12日发布的文献IETF RFC33中发现的,撰写者为Stephen Carr、Steve Crocker和Vint Cerf。根据美国计算机历史博物馆的记载,Croker写道:“命名空间的元素都可称为套接字接口。一个套接字接口构成一个连接的一端,而一个连接可完全由一对套接字接口规定。”计算机历史博物馆补充道:“这比BSD的套接字接口定义早了大约12年。”

3、socket的基本操作

既然socket是“open—write/read—close”模式的一种实现,那么socket就提供了这些操作对应的函数接口。下面以TCP为例,介绍几个基本的socket接口函数。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324681190&siteId=291194637