Java programming Those things 104--network programming Technology 3

Source: Internet
Author: User

In order to master the Network programming step by step, the following research two basic problems in network programming, by solving these two problems will be to the understanding of network programming deep one layer.

1, how to reuse socket connection?

In the previous example, the client set up a connection, sent only once the data is closed, which is equivalent to making a phone call, the phone through a dialogue only once shut down, in fact, more commonly used should be dialed a phone after a number of conversations, which is the multiplex client connection.

So how do you create a connection and do multiple data exchanges? In fact, it is very simple, after establishing the connection, the logic of the data exchange is written in a loop. This allows the connection to not be closed as long as the loop does not end. In this way, you can change the above code, so that the program can be established after the connection once, send three data, of course, the number can also be several times, the sample code is as follows:

package TCP;
Import java.io.*
Import java.net.*
/**
* Multiplexing connected socket Client
* Function: Send string "Hello" To the server side and print out server-side feedback
*/
public class Mulsocketclient {
public static void Main (string[] args) {
Socket Soc ket = null;
InputStream is = null;
OutputStream OS = null;   
//server-side IP address
String serverip = "127.0.0.1";
  Server-side port number
int port = 10000;
  Send content
String data[] ={"A", "Second", "Third"};
   try {
//Establish connection
Socket = new socket (serverip,port);
   Initializes the stream
OS = Socket.getoutputstream ();
is = Socket.getinputstream ();
Byte[] b = new byte[1024];     
for (int i = 0;i < data.length;i++) {
//Send data
Os.write (Data[i].getbytes ());
    Receive data
int n = is.read (b);
   Output Feedback Data
System.out.println ("Server Feedback:" + new String (b,0,n));

} catch (Exception e) {
E.printstacktrace ();///Print exception information
}finally{
try {
//close stream and connection
Is.close ();
Os.close ();
Socket.close ();
} catch (Exception E2) {}
}
}
}

The example program writes the logic of the data interchange portion in a for loop, in contrast to the preceding code, so that a connection can be established, sequentially sending the data in the array to the server.

If you still run the program using the server-side program in the previous sample code, the result of the program is:

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.net.SocketInputStream.read(SocketInputStream.java:90)
at tcp.MulSocketClient.main(MulSocketClient.java:30)

Server Feedback:

Obviously, there was an exception when the client was actually running, what was the cause of the exception? If you read the previous code carefully, you should remember that the server side of the previous example code closes the connection once after the conversation, and if the server-side program shuts down, the client continues to send data that is definitely abnormal, which is why this problem occurs.

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.