code example for Java Socket Communication

Source: Internet
Author: User
Tags flush getmessage readline socket client

Clientapp.java original file to implement client-side functionality:

Import java.net.*;
Import java.io.*;
Import java.lang.*;

public class ClientApp
{
public static void Main (String args[])
{
Try
{
Create communications and connect to the host rock
Socket csocket=new socket ("192.168.100.188", 8018);
Open the input/output stream for this socket
OutputStream Os=csocket.getoutputstream ();
DataInputStream is=new DataInputStream (Csocket.getinputstream ());

int C;
Boolean flag=true;

String Responseline;

while (flag)
{
Accept characters from standard input output and write as System
while ((C=system.in.read ())!=-1)
{
Os.write ((byte) c);
if (c== ' \ n ')
{
Os.flush ();
Block the program until the reply is received and display them on the standard output
Responseline=is.readline ();
SYSTEM.OUT.PRINTLN ("message is:" +responseline);
}
}
}
Os.close ();
Is.close ();
Csocket.close ();

}
catch (Exception e)
{
System.out.println ("Exception:" + e.getmessage ());
}
}
}


Serverapp.java original file that implements server-side functionality:

Import java.net.*;
Import java.io.*;

public class Serverapp
{
public static void Main (String args[])
{
Try
{
Boolean flag=true;
Socket Clientsocket=null;
String Inputline;
int C;

ServerSocket ssocket=new ServerSocket (8018);
System.out.println ("Server Listen On:" +ssocket.getlocalport ());

while (flag)
{
Clientsocket=ssocket.accept ();
DataInputStream is= New DataInputStream (New Bufferedinputstream (Clientsocket.getinputstream ()));
OutputStream Os=clientsocket.getoutputstream ();

while ((Inputline=is.readline ())!=null)
{
The server program terminates when the client enters the stop!
if (Inputline.equals ("Stop"))
{
Flag=false;
Break
}
Else
{
System.out.println (Inputline);

while ((C=system.in.read ())!=-1)
{
Os.write ((byte) c);
if (c== ' \ n ')
{
Os.flush (); Sending information to the client
Break
}
}
}


}
Is.close ();
Os.close ();
Clientsocket.close ();

}
Ssocket.close ();
}
catch (Exception e)
{
System.out.println ("Exception:" + e.getmessage ());
}
}
}






Related Article

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.