Java Socket Communication (i) client program sends and receives data _java

Source: Internet
Author: User

The network application is divided into two parts: the client and the service side, while the socket class is the Java class responsible for handling client communication. This class enables you to connect to a server with a specified IP or domain name, and can send and receive data to and from the server.

For the socket communication brief, the service end to the socket output stream inside write something, the client can through the socket input stream to read the corresponding content. Socket and socket is two-way connectivity, so the client can also to the corresponding socket output stream inside write things, and then the corresponding socket of the service side of the input stream can read the corresponding content.

Example 1: Shorthand for Client (a).

Socket client = null;
try{
client = new Socket (ip,port);
String msg= "The content of the data sent!" ";
Gets the socket read-write stream and sends the data 
Client.getoutputstream () to the server program. Write (Msg.getbytes ());
byte[] datas = new byte[2048];
Receive data from a server-side program
Client.getinputstream (). read (datas);
System.out.println (New String (datas));
catch (Exception e) {
e.printstacktrace ();
} Finally {
if (client!= null) {
try {
client.close ();
} catch (IOException e) {
System.out.println ("Systemerr:" +e);}}}

Example 2: Client Shorthand (ii).

try{
client = new Socket ();
SocketAddress socketaddress = new inetsocketaddress (ip,port);
Client.connect (socketaddress, 3000);
String msg= "Accessed server returns content!" ";
Gets the socket read-write stream and sends the data 
Client.getoutputstream () to the server program. Write (Msg.getbytes ());
byte[] datas = new byte[2048];
Receive data from a server-side program
Client.getinputstream (). read (datas);
System.out.println (New String (datas));
catch (Exception e) {
e.printstacktrace ();
} Finally {
if (client!= null) {
try {
client.close ();
} catch (IOException e) {
System.out.println ("Systemerr:" +e);}}}

Example 3: The complete writing of the client.

try { 
//1. Establish a client socket connection, specify the server location and port 
socket sockets =new socket (ip,port); 
2. Get socket read-write stream 
outputstream os=socket.getoutputstream (); 
PrintWriter pw=new printwriter (OS); 
Input stream 
InputStream is=socket.getinputstream (); 
BufferedReader br=new BufferedReader (New InputStreamReader (IS)); 
3. Use the flow according to certain operation, the socket for read 
and write operation String sendinfo= "Send data information to the server!" ";
Pw.write (sendinfo); 
Pw.flush (); 
Socket.shutdownoutput (); 
The corresponding String replyinfo=null of the receiving server 
; 
while (!) ( (Replyinfo=br.readline ()) ==null)) { 
System.out.println ("Data information for the receiving server:" +replyinfo); 
} 
4. Close Resource 
br.close (); 
Is.close (); 
Pw.close (); 
Os.close (); 
Socket.close (); 
} catch (Unknownhostexception e) { 
e.printstacktrace (); 
} catch (IOException e) { 
e.printstacktrace (); 
}

About Java Socket Communication (i) of the client program to send and receive data related knowledge, small series to introduce to you here, more information please visit the Cloud Habitat Community website to learn more!

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.