Socket multithreaded Programming ServerSocket Server Multithreaded programming

Source: Internet
Author: User

Socket multithreaded Programming ServerSocket Server Multithreaded programming

Server code

Package com.lotus.server;

Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Timer;
Import Java.util.TimerTask;

public class Socketserver {
List<socket> list;
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Socketserver socketserver=new socketserver ();
Socketserver.startserver ();
if (Thread.activecount () >1) {
Thread.yield ();
}


}



public void StartServer () {
ServerSocket Serversocket=null;
try {
List=new arraylist<socket> ();
Serversocket=new ServerSocket (9898);
New Meldsocket (ServerSocket). Start ();

} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

Class Meldsocket extends thread{
Private ServerSocket ServerSocket;
Public Meldsocket (ServerSocket serversocket) {
This.serversocket=serversocket;
}

@Override
public void Run () {
TODO auto-generated Method Stub
Socket socket;
while (true) {

try {
Socket = Serversocket.accept ();
New Readerthread (socket). Start ();
New Writerthread (socket). Start ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}

Class Writerthread extends thread{
Private Socket Socket=null;
BufferedReader Inputreader=null;
BufferedWriter Writer=null;
Public writerthread (socket socket) {
This.socket=socket;
}
@Override
public void Run () {
TODO auto-generated Method Stub

try {
System.out.println ("Start input");
Writer=new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ()));
Inputreader=new BufferedReader (New InputStreamReader (system.in));
New Timer (). Schedule (new TimerTask () {

@Override
public void Run () {
TODO auto-generated Method Stub
try {
Writer.write ("Lao Tzu is doing the test \ n");
Writer.flush ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}, 1000, 10000);
String inputcontent;
while ((Inputcontent=inputreader.readline ()) = = "Byte") {
System.out.println (inputcontent);
//
//}

} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Class Readerthread extends thread{
Private Socket Socket=null;
Bufferedinputstream Reader=null;
Public readerthread (socket socket) {
This.socket=socket;
}
@Override
public void Run () {
TODO auto-generated Method Stub

try {

Reader=new Bufferedinputstream (Socket.getinputstream ());
System.out.println ("received input information");

int len=0;
Byte[] Bty=new byte[1024];
while (len = Reader.read (bty))! =-1) {
String Inputcontent=new string (Bty,0,len);
System.out.println (inputcontent);
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
Reader.close ();
Socket.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
}

Client code

Package com.lotus.pushlangconnect;

Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import Java.net.Socket;

Import javax.sound.sampled.AudioFormat.Encoding;

public class Socketclient {

Public Socketclient () {

}
public static void Main (string[] args) throws IOException {
Socketclient client=new socketclient ();
Client.start ();
}

public void Start () throws ioexception{
Socket Socket=null;
try {
Socket=new Socket ("localhost", 9898);
New Readerthread (socket). Start ();
New Writerthread (socket). Start ();

} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}


}
Class Writerthread extends thread{
Private Socket Socket=null;
BufferedReader Inputreader=null;
BufferedWriter Writer=null;
Public writerthread (socket socket) {
This.socket=socket;
}
@Override
public void Run () {
TODO auto-generated Method Stub

try {
System.out.println ("Start input");
Writer=new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ()));
Inputreader=new BufferedReader (New InputStreamReader (system.in));
String inputcontent;
while ((Inputcontent=inputreader.readline ()) = = "Byte") {
System.out.println (inputcontent);
Writer.write (inputcontent);
Writer.flush ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
Writer.close ();
Inputreader.close ();
Socket.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}

Class Readerthread extends thread{
Private Socket Socket=null;
BufferedReader Reader=null;
Public readerthread (socket socket) {
This.socket=socket;
}
@Override
public void Run () {
TODO auto-generated Method Stub

try {

Reader=new BufferedReader (New InputStreamReader (Socket.getinputstream ()));

String respsonsecontent;
while ((Respsonsecontent=reader.readline ())!=null) {
System.out.println (respsonsecontent);
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
Reader.close ();
Socket.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
}

Socket multithreaded Programming ServerSocket Server Multithreaded programming

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.