Socket-based API and parsing of java programming

A, socket communication process

1, socket and socket programming Description:

socket is translated as "socket", which is an agreement or a way to communicate between computers. By this convention socket, a computer may receive data from other computers, and may also send data to other computers.

We are talking about socket programming, it is standing on the underlying transport layer, so you can use TCP / UDP protocol, but can not access the page because the http protocol to access web pages require the application layer. As an application is to implement the content of the layer below, it can not be achieved on the content of the layer.

2, socket communication process:

(1) Create a ServerSocket and Socket

(2) Open Socket connection to an input / output streams

(3) Socket according to the protocol of read / write operations

(4) closing the input and output streams, Close Socket

 

2, the difference between linux and windows in the socket socket file.

As the socket socket, after transmitting the data can be packaged, is essentially a socket file.

Linux is the socket file as a regular file, and the file will be seen in the windows socket is a network connection to deal with, so you need to call the data transmission function designed specifically for the socket.

 

Second, based on java socket programming

Using a client and server use the console shown in the following code. Java-based Socket programming practice to call the java's main Socket and ServerSocketAPI, for the server needs to bind ServerSocket class, after listening accept method interface binding, and for the client, send data after binding interface uses.

1, client communication process

(1) create a Socket object indicating the need to connect to the server's address and port number

(2) connection is established, the output stream by the server sends a request message like

(3) obtain the information server in response to an input stream by

(4) closing response resources

 

import java.io.IOException;

import java.io.PrintWriter;

import java.net.InetAddress;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Scanner;

 

public class Client{

    public static void testClient(){

       System.out.println ( "request to the server is connected to ...");

       Socket socket = null;

       Scanner keybordscanner = null;

       Scanner inScanner = null;

       PrintWriter pwtoserver = null;

       try {

// using InetAddress class to get connected to all Socket Programming

           socket = new Socket(InetAddress.getLocalHost(),6668);

           inScanner = new Scanner (socket.getInputStream ());

           System.out.println(inScanner.nextLine());

           pwtoserver = new PrintWriter(socket.getOutputStream());

           System.out.print ( "I (client):");

           keybordscanner = new Scanner(System.in);

           while(keybordscanner.hasNextLine()){

              String keyborddata = keybordscanner.nextLine();

              // the message sent by the client

              System.out.println ( "I (client):" + keyborddata);

              // write to the server's console

              pwtoserver.println(keyborddata);

              pwtoserver.flush();

              // blocked messages waiting to receive the service side

              String indata = inScanner.nextLine();

              System.out.println ( "server:" + indata);

              System.out.print ( "I (client):");

           }

       } catch (UnknownHostException e) {

           // TODO Auto-generated catch block

           e.printStackTrace ();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace ();

       }finally {

           keybordscanner.close();

           pwtoserver.close();

           inScanner.close();

           try {

              socket.close();

           } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace ();

           }

       }

      

    }

    public static void main(String[] args) {

       testClient();

    }

}

 

 

 

 

2, the server:

(1) Create a ServerSocket object and bind listening port

(2) accept () method listen for client requests

(3) connection is established, the client sends a read request information through the input stream

(4) transmits information via an output flow accent client

(5) close Related Resources

 

import java.io.IOException;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Scanner;

 

public class Server{

    //server

    public static void testServer(){

       // Create a server

       System.out.println ( "wait for the client to connect ...");

       PrintWriter pwtoclien = null;

       Scanner keybordscanner = null;

       Scanner inScanner = null;

       ServerSocket ss = null;

       try {

           ss = new ServerSocket(6667);

           // create an object receives a connection to the client

           Socket socket = ss.accept();

           System.out.println (socket.getInetAddress () + "has been successfully connected to this server.");

           // character-output stream

           pwtoclien = new PrintWriter(socket.getOutputStream());

           pwtoclien.println ( "! has been successfully connected to a remote server" + "\ t" + "Please speak first.");

           pwtoclien.flush();

           keybordscanner = new Scanner(System.in);

           inScanner = new Scanner (socket.getInputStream ());

           // blocked waiting for clients to send messages over

           while(inScanner.hasNextLine()){

              String indata = inScanner.nextLine();

              System.out.println ( "Client:" + indata);

              System.out.print ( "I (server):");

              String keyborddata = keybordscanner.nextLine();

              System.out.println ( "I (server):" + keyborddata);

              pwtoclien.println(keyborddata);

              pwtoclien.flush();

           }

            socket.shutdownInput();

            socket.shutdownOutput();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace ();

       }finally {

           pwtoclien.close();

           keybordscanner.close();

           inScanner.close();

           try {

              ss.close();

           } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace ();

           }

       }

    }

    public static void main(String[] args) {

       test server ();

    }

}

 

 

Here is single-threaded communications. Order and uses a blocking printWriter.flush way to control communication: the client after the server sends a message to reply to a message, send the message again only when the server to the client in order to respond to the client, neither form when sending files deadlock, deadlock can only be lifted after sending files.

Console application server as shown, using the acquired transmission InetAddress classes Address file. In addition, the programming process, in both classes is easy to get confused with GetInputSream GetOutputStream, the former is used to read, which is used to write.

 

Client: A client as shown in the display console.

 

 

 

To observe the java server function call in linux

Strace command is used to track calls

 

 

 

 Three, linux Network Interface Overview

To establish a connection in linux

站在更贴近系统的层级去看,两个机器间的通信方式,无非是要通过运输层的TCP/UDP,网络层IP,因此socket本质是编程接口(API),对TCP/UDP/IP的封装,TCP/UDP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket编程接口。
Socket的创建:

int socket (int domain, int type, int protocol);

创建一个服务器端的socket

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

我们套接字已经创建好了,地址结构也已经了解了,接下来就是要将套接字和地址进行关联,关联的方法则是通过bind函数。

 

int bind(int sockfd, const struct sockaddr *addr, socklen_t len);

 

我们的socket已经创建出来了,当我们不再使用的时候,我们可以调用close函数来将其关闭,释放该文件描述符,这样便可以得到重新的使用。
套接字通信是双向的,但是,我们可以采用shutdown函数来禁止一个套接字的I/O

 
进行连接

#include <sys/socket.h>int connect(int sockfd, const struct sockaddr *addr, socklen_t len);

关闭连接

int shutdown(int sockfd, int how);

 

Guess you like

Origin www.cnblogs.com/yyl666/p/11982190.html
Recommended