The communication program of Java writing TCP mode

Source: Internet
Author: User
Tags exit net socket string connection reset thread client
Program

A large number of functions are encapsulated in the Java API for use when writing a network communication program.
This enables Java to have a powerful network capability.
Using Java to write a TCP communication program is simpler, but there are some issues to be aware of.

The following is the listener main program, which, after discovering the client connection, initiates a session socket thread to enable real-time sending and receiving of information
Work with multiple clients at the same time.
Import java.io.*;
Import java.lang.*;
Import Java.net.ServerSocket;
Import Java.net.Socket;
The main program is always listening, and a connection starts a thread for processing to implement multiple clients
public class Listenserve
{
Private ServerSocket SS;
Private Boolean listening=true;
Public Listenserve ()
{
Init ();//initialization
LISN ()//Start monitoring
}
public void Init ()
{
Try
{
Ss=new ServerSocket (10015,10);
}
catch (IOException IE)
{
System.out.println ("Unable to monitor on port 10015");
Ie.printstacktrace ();
}
}
public void Lisn ()
{
Try
{
while (listening)
New Thread (New Dialogserve (ss.accept)). Start ();
}
catch (IOException IE)
{Ie.printstacktrace ();}
}
public static void Main (String args[])
{
New Listenserve ();
}
}

The following is the session main program
It should be noted that if the client first closes, the session socket may throw socketexception:connection reset
This should be handled in the program, which is also a more easily overlooked issue.
Import java.io.*;
Import java.lang.*;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import java.net.SocketException;
public class Dialogserve implements Runnable
{
Private Socket S;
Private InputStream in;
Private String rev,temp;
private byte b[];
private int len;
Public Dialogserve (Socket SS)
{
S=ss;
B=new byte[1024];
Try
{
In=s.getinputstream ();
}catch (IOException IE)
{
Ie.printstacktrace ();
}
Rev= "";
}
public void Run ()
{
Try
{
while (s.isconnected () ==true)
{
if ((Len=in.read (b))!=-1)
{
Temp=new String (B,0,len);
Rev+=temp;
System.out.print (rev);
Temp=null;
Thread.Sleep (1000);
}
}
In.close ();
S.close ();
SYSTEM.OUT.PRINTLN (The session socket is disconnected!) ");
}
catch (SocketException se)
{
SYSTEM.OUT.PRINTLN ("Client is disconnected!") ");
System.exit (0);
}
catch (IOException io)
{
Io.printstacktrace ();
System.exit (0);
}
catch (Interruptedexception IRE)
{Ire.printstacktrace ();}
}
}
The following is the client main program
Import java.io.*;
Import Java.net.Socket;
Import java.lang.*;
public class Client
{
Private socket con;//Client connection socket
Private OutputStream out;
Private String Sen;
private byte b[];
Public Client ()
{
Clientinit ();
}
public void Clientinit ()
{
Try
{
Con=new Socket ("localhost", 10015);
Con.setsotimeout (10000);
B=new byte[1024];
OutputStream Out=con.getoutputstream ();
sen= "Hello serve, send data in TCP mode!" ";
B=sen.getbytes ();
Out.write (b);
Out.flush ();
Out.close ();
Con.close ();
}
catch (IOException IE)
{
Ie.tostring ();
}
}
public static void Main (String args[])
{
New Client ();
}
}
Generally speaking, the above listed code is simpler, but has basically reflected the Java writing Simple TCP communication program principle.
I hope you will criticize your friends.




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.