Java basics Socket Network Programming Application Example

Source: Internet
Author: User

Speaking of the content in the java Network Communication Section, the beginner may feel a headache. It should be because the Socket communication will always be accompanied by IO stream operations, of course, those familiar with IO streams will think that this is a very interesting chapter, because everything is under their control, and this operation will become very handy, however, I/O is not familiar to many buddies, and it is difficult to understand the IO stream operation mechanism and apply it to Socket communication, otherwise, you will be very depressed and annoyed with the results. I will share with you my feelings and solutions after a little trouble.

Requirement: the client uses Socket Communication Technology to upload a local text file to the server (instead of manually entering text on the console ), the server stores the text uploaded by the client in another text file. After the file is saved, the server sends a "saved" notification to the client. The client displays the information returned by the server and disconnects the client from the server, now the entire communication has ended.

The code for completing this task is as follows:

1. Client:

Import java. io. BufferedReader;
Import java. io. FileReader;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. PrintWriter;
Import java.net. InetAddress;
Import java.net. Socket;
Import java.net. UnknownHostException;

Public class ClientSocket {

Public static void main (String args []) throws UnknownHostException, IOException

{

System. out. println ("the client has started ...........");

// 1. Create a client Socket object and specify the IP address and port number of the server to be connected.

Socket s = new Socket (InetAddress. getByName ("192.168.56.4"), 10005); // The connection failure exception is thrown here. A solution is provided based on your needs.

}

}

2. Server Side

Import java. io. BufferedReader;
Import java. io. FileWriter;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. PrintWriter;
Import java.net. ServerSocket;
Import java.net. Socket;

Public class ServersSocket {
Public static void main (String [] args) throws IOException {
System. out. println ("the server has started ....");
// 1. Create a Server Object and bind the listening port number
ServerSocket ss = new ServerSocket (10005 );
// 2. Obtain the client object
Socket s = ss. accept ();
// 3. Obtain the Client IP address and client name
String ip = s. getInetAddress (). getHostAddress ();
String name = s. getInetAddress (). getHostName ();
System. out. println ("received connections from the name" + name + "and ip:" + ip + ");
// 4. Obtain the IO Data Source
BufferedReader brfu = new BufferedReader (new InputStreamReader (s. getInputStream ()));
// 5. Set the purpose to create a local text file
PrintWriter out = new PrintWriter (new FileWriter ("server.txt"), true );

// The above code is equivalent to BufferedWriter bw = new BufferedWriter (new FileWriter ("server.txt "))

// 6. Start the data saving operation
String line = null;
While (line = brfu. readLine ())! = Null ){

Out. write (line + "\ r \ n ");
System. out. println (line );
}
// 7. Create a Server Source: Network
PrintWriter outSer = new PrintWriter (s. getOutputStream (), true );
OutSer. println ("accepted and saved completely! ");

// 8. Disable IO and network resources.
Out. close ();
S. close ();
Ss. close ();

}

}

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.