/*
When developing a Web application, if the data is sent using a link to the Outoutstream->bufferedoutputstream stream, then
Be sure to call flush when the data is sent or close the link stream otherwise the receiver may not receive the data ...
The data will not be sent until the buffer is full.
*/
Package me;
Import java.net.*;
Import java.io.*;
public class Javanet extends Thread
{
Private Socket SS;
public static void Main (String []args)
{
if (args.length>0)
Runserver ();
Else
Runclient ();
}
Javanet (Socket ss) throws Exception
{
this.ss=ss;
INP Utstream in= Ss.getinputstream (); //Get input stream
outputstream out=ss.getoutputstream (); //Get output stream
out.write ("Hellow Welcome". GetBytes ()); //write data to output stream
byte ch[] =new byte[100];< br> int Num=in.read (CH);
System.out.println (New String (Ch,0,num));
in.close ();
out.close ();
ss.close ();
}
public void Run ()
{
}
static public void Runserver () //service side
{
try
&nbs P {
serversocket s1 = new ServerSocket (6666);
while (true)
{
Socket ss=s1.accept (); /Waiting for the arrival of the connection
new Javanet (ss). Start ();
}
}catch (Exception e)
{
E.printstacktrace ();
}
}
static public void Runclient ()//Client
{
try{
Socket ss=new Socket (inetaddress.getbyname ("127.0.0.1"), 6666); Getbyname NULL returns local IP "LocalHost" returns local IP also returns local
OutputStream Out=ss.getoutputstream (); Get output flow to server write data
Out.write ("I am Zhangsan". GetBytes ());
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}