Network Programming--Designing a program to send TCP packets

Source: Internet
Author: User
Tags socket
Summary

The TCP/IP protocol family is the cornerstone protocol for building the Internet. In the Internet, data is transferred from one computer to another, and packets can be transmitted through two protocols. One of these is the transmission Control Protocol (transmission protocol,tcp). TCP ensures the delivery of data and ensures that packets are delivered in the correct order, and is a transport protocol that provides a reliable connection. Because the Java language supports stream-based communication, which is transmitted using the TCP protocol, this course design will design a program that can send TCP packets based on the Java language. Key Words

Transmission Control Protocol (TCP), Java, socket (socket), IP, Port 1 Introduction

In the four layer protocol of the Internet, TCP is located above the IP layer and the transport layer below the application layer. There is often a need for reliable, pipe-like connections between the application tiers of different hosts. The TCP protocol, which is connection-oriented and provides reliable transmission, can meet this requirement. It is therefore widely used.

Mastering the principle and function of TCP packet package is helpful for us to learn and utilize more application layer applications and technologies such as file Transfer (FTP), remote login (RemoteLogin) and so on.

This course design requires that we achieve the following effects:

(1) Run as command line:

Sendtcp source_ip source_port dest_ip dest_port

Where Sendtcp is the program name, SOURCE_IP is the source IP address, Source_port is the source port, DEST_IP is the destination IP address, and Dest_port is the destination port.

(2) Other TCP header parameters should be set by yourself.

(3) The data field is "Thisis my homework of network, I am happy!".

(4) Output "Sendok" on the screen after successful transmission. 2 overall Design 2.1 system or algorithmic framework design (1) The design framework of the service side (send data segment side)
(2) Design framework of client (send feedback Party)

2.2 Functional Design

In this course design, it is required to send a packet containing a piece of data information. Also, validation is required in order to verify that the packet was sent successfully. So you need to implement at least two features that send packets and verify that the send is successful. Establishes the server sends the data segment, the client receives the data segment and sends the feedback information. When they are running on separate hosts, run the program. These two functions can be verified according to the operation of the program.


On the server side, we first establish the monitoring of the source port. When the client successfully connects to the source port, the server sends the data segment as required "This is my homework of network, I amhappy!" To the destination IP, port. Client, new port listener, send "sendok!" when data segment is received Feedback to the service side. The above two functions can be realized by the data transmitted by such packets, as shown in Figure 3. Note that the server source IP, port is the client's destination IP, port, the destination IP of the server, port is the client's source IP, port.


2.3 Platform Design

This course is designed to be implemented on the Java language platform using the Java language. In order to implement the ability to send packets on both computers. This experiment uses the physical machine and the virtual machine simulation in the same local area network. The physical machine takes the Windows 8.1 system, the IP address is 192.168.126.53, and the virtual machine uses a Windows XP system with an IP address of 192.168.126.52. As shown in Figure 4. 2.4 Design of data structure

This experiment takes a string type to hold the character, while the port number takes the int type. Because text I/O requires encoding and decoding, the efficiency of binary I/O is more efficient than text I/O. Therefore, it is best to use binary I/O to transfer data between the server and the client in order to improve efficiency. Thus, the two classes of DataInputStream and Dataoutstrea are used in read-in and write-up respectively. 2.5 Interface Design

According to the requirements of the topic, the program must enter 4 parameters when running: Source IP, source port, destination IP, destination port. The program starts by judging if 4 parameters have been entered, if no four parameters are entered, or an error is entered, the output error is shown in Figure 5.
3. Detailed design (according to the actual situation, the details of the entire design process and the core source code) 3.1 parameter read-in

Both the client and the server are required to read IP and port information as required. and the server source IP, port is the client's destination IP, port; The destination IP of the server, port is the source IP, port of the client. [Java] view plain copy print? String SourceIP = args[0];//Source IP int sourceport = integer.parseint (args[1]);//Source port String Destinationip = args[2 ];//Destination IP int destinationport = integer.parseint (args[3]);//Destination Port

String SourceIP = args[0];//Source IP
    int sourceport = Integer.parseint (args[1]);//Source port
    String Destinationip = args[2] ;//Destination IP
    int destinationport = Integer.parseint (args[3]);//Destination Port
3.2 Setting up sockets

          a socket (socket) is required for both the server and the client. There are two ways to set up sockets, one is to send information proactively. The exception is the passive reception of information to send back feedback (listening). In the course of the experiment, both ends need to be used to both the active information and the use of monitoring. [Java] view plain copy print? Unsolicited message, destination port for destination IP    socket socket = new socket (destinationip,  Destinationport);   dataoutputstream dataoutputstream    = new  DataOutputStream (Socket.getoutputstream ());   Dataoutputstream.writeutf ("Response Please!");   //Listen, receive source port information sent to source IP.    Serversocket serversocket = new serversocket (sourceport);        socket = serversocket.accept ();  

Unsolicited message, send direction for destination IP Port
Socket socket = new socket (Destinationip, destinationport);
DataOutputStream DataOutputStream 
= new DataOutputStream (Socket.getoutputstream ());
Dataoutputstream.writeutf ("Response please!");
Listen and receive source port information sent to the source IP.
ServerSocket serversocket = new ServerSocket (sourceport);
    Socket = Serversocket.accept ();
3.3 Sending or receiving data

          As the focus of this course experiment, the success of sending and receiving is the key to the success of this course design. In order to ensure that the program can run successfully, the data sent in this case uses a binary data stream. [Java] view plain copy print?     //Send Message    dataoutputstream dataoutputstream    = new  dataoutputstream (Socket.getoutputstream ());   Dataoutputstream.writeutf ("Response Please!");    //Receive information    datainputstream datainputstream                             = new datainputstream (Socket.getinputstream ());                    string datasegment= Datainputstream.readutf ();  

	Send Message
dataoutputstream dataoutputstream 
= new DataOutputStream (Socket.getoutputstream ());
Dataoutputstream.writeutf ("Response please!"); 
Receive information
datainputstream datainputstream
                        = new DataInputStream (Socket.getinputstream ());
                String Datasegment=datainputstream.readutf ();
3.4 Service-side function Realization

Service side in this course design, mainly responsible for the destination socket (destination ip+ port) Send data segment "Thisis My homework of the network, I am happy! "and receive the data segment" sendok! "returned by the destination socket, as shown in Figure 6.[Java] View Plain copy print? Record source and destination sockets information    string sourceip = args[0];   int sourceport =  integer.parseint (args[1]);   string destinationip = args[2];   int  destinationport = integer.parseint (args[3]);     //Set up monitoring    Serversocket serversocket = new serversocket (sourceport);   Socket socket  = serversocket.accept ();     //Receive information from clients    datainputstream  datainputstream                            = new datainputstream ( Socket.getinputstream ());  //Send info    string datasegment =  "this is  My homework of network ,i am happy! ";    dataoutputstream dataoutputstream    = new dataoutputstream (Socket.getoutputstream ());    Dataoutputstream.writeutf (datasegment);                     //Receive Success Information    Socket = new socket ( Destinationip, destinationport);   Dataoutputstream = new dataoutputstream ( Socket.getoutputstream ());   Dataoutputstream.writeutf ("successfully received!");       Datainputstream = new datainputstream (Socket.getinputstream ());    Datasegment=datainputstream.readutf ();   printmessage ("Received from"  + socket.getinetaddress (). toString (). substring (1)  +  ","                             +  Socket.getport ()  +  "Port Information");   printmessage ("Information contentAs: " + datasegment);     //Close stream    datainputstream.close ();   Dataoutputstream.close ();   socket.close ();   serversocket.close ();  

Information for recording source and destination sockets String SourceIP = Args[0];
int sourceport = Integer.parseint (args[1]);
String Destinationip = args[2];

int destinationport = Integer.parseint (args[3]);
Establish monitoring ServerSocket ServerSocket = new ServerSocket (sourceport);

Socket socket = serversocket.accept ();
Receive information from the client datainputstream DataInputStream = new DataInputStream (Socket.getinputstream ());
Send Message String datasegment = "This is my homework of network, I am happy!"; 
DataOutputStream DataOutputStream = new DataOutputStream (Socket.getoutputstream ());
               
Dataoutputstream.writeutf (datasegment);
Receive success information socket = new socket (Destinationip, destinationport);
DataOutputStream = new DataOutputStream (Socket.getoutputstream ());

Dataoutputstream.writeutf ("Successfully received!");
DataInputStream = new DataInputStream (Socket.getinputstream ());
Datasegment=datainputstream.readutf (); Printmessage ("received from" + socket.getinetaddress (). toString (). substring (1) + "," + sockEt.getport () + "port Information");

Printmessage ("Information content is:" + datasegment);
Close flow datainputstream.close ();
Dataoutputstream.close ();
Socket.close (); Serversocket.close ();
3.5 client Function Implementation

Client in this course design, the main responsibility is to receive information from the server, and send the feedback message "Send ok!", see Figure 7.
[Java] View Plain copy print?     //trying to connect            socket socket  = new socket (destinationip, destinationport);            DataOutputStream dataOutputStream    = new dataoutputstream ( Socket.getoutputstream ());            Dataoutputstream.writeutf ("response please!");          //Get response    datainputstream datainputstream   =  new datainputstream (Socket.getinputstream ());   string datasegment =  Datainputstream.readutf ();   printmessage ("Received from"  + socket.getinetaddress (). toString (). SUBSTRING (1)  +  ","                             + socket.getport ()  +  "Port Information");            Printmessage ("Information content:"  + datasegment);     //reply    serversocket  Serversocket = new serversocket (Sourceport);   socket = serversocket.accept ( );   Dataoutputstream = new dataoutputstream (Socket.getoutputstream ());   Dataoutputstream.writeutf ("send ok!");   

	Attempt to connect
   		socket socket = new socket (Destinationip, destinationport);
        DataOutputStream DataOutputStream 
= new DataOutputStream (Socket.getoutputstream ());
        Dataoutputstream.writeutf ("Response please!");

   Get response
datainputstream datainputstream
= new DataInputStream (Socket.getinputstream ());
String datasegment = Datainputstream.readutf ();
Printmessage ("received from" + socket.getinetaddress (). toString (). substring (1) + ","
                        + socket.getport () + "port Information");
        Printmessage ("Information content is:" + datasegment);

Reply
ServerSocket serversocket = new ServerSocket (sourceport);
Socket = Serversocket.accept ();
DataOutputStream = new DataOutputStream (Socket.getoutputstream ());
Dataoutputstream.writeutf ("Send ok!");
4 Summary 4.2 problems that exist

(1) The command line interface is not beautiful and the operation is not strong enough

(2) Input 4 parameters at a time it's too much trouble. 4.3 Improved methods

(1) Use the graphical interface and beautify the interface. But it may not be fast enough for now.

(2) Follow the command prompt (CMD), which can be entered by default. "References"

[1] Y.daniel Liang.java Language Programming (Basic) [M]. Li Na, translated. Beijing: Machinery Industry Press, 2011:527-535

[2] Y.daniel Liang.java Language Programming (Advanced) [M]. Li Na, translated. Beijing: Machinery Industry Press, 2011:258-264

[3] Shehiren. Computer network Concise Tutorial (second edition) [M]. Beijing: Electronic Industry Press, 2013:123-143





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.