Joke Collection Website - Public benefit messages - Java multithreading to achieve file transfer between the server and the client code! ! ! Urgent! ! ! !
Java multithreading to achieve file transfer between the server and the client code! ! ! Urgent! ! ! !
Once a client connects, the server opens the listening port and creates two new threads to take charge of the connection.
One is responsible for the information sent by the client (ClientMsgCollectThread class),
The other is responsible for sending data through Socket (ServerMsgSendThread).
Server.java code is as follows:
/*
* Date of creation: March 7, 2009
*
* TODO To change the template of this makefile, go to.
* Window-Preferences -Java- Code Style-Code Template
*/
Packaging failure. Multi-user;
Import java.io.bufferedreader;
Import java.io.ioexception;
Import java.io.inputstreamreader;
Import java.io.printwriter;
Import java.net.serversocket;
Import java.net.socket;
/**
* server side
*
* @ Author Faue
*/
Public class server extension ServerSocket {
Private static final int server _ port =10000;
/**
* construction method, used to realize the monitoring of the connection.
*
* @ throw IOException
*/
The public server () threw IOException {
Super (server _ port);
Try {
while (true) {
socket socket = super . accept();
New thread (new ClientMsgCollectThread(socket), "getAndShow"
+ socket.getPort())。 start();
New thread (new server MsgSendThread), "Send"
+ socket.getPort())。 start();
}
} catch (IOException e) {
e . printstacktrace();
}
}
Public static void main(String[] args) throws IOException {
New server ();
}
/**
* This class is used to create a thread that receives information sent by the client and displays it.
*
* @ Author Faue
* @ version 1.0.0
*/
ClientMsgCollectThread class implements Runnable {
Private socket client;
Private BufferedReader in
private string buffer inputstring buffer = new string buffer(" Hello ");
/**
* get the input stream of the socket
*
* @param s
* @ throw IOException
*/
The public ClientMsgCollectThread (sockets) throws IOException {
Client = s;;
in = new buffered reader(new InputStreamReader(client
. getInputStream()," GBK "));
}
Public invalid operation () {
Try {
And (! client.isClosed()) {
inputStringBuffer.delete(0,inputstringbuffer . length());
inputstringbuffer . append(in . readline());
system . out . println(getMsg(inputstring buffer . tostring()));
}
} catch (IOException e) {
//e . printstacktrace();
System. out.println (client.tostring ()+"Closed!" );
}
}
/**
* construct the displayed string
*
* @param line
* @ Return
*/
Private string getMsg (string line)
Return client.toString()+"means:"+line;
}
}
/**
* This class is used to create a thread to send data.
*
* @ Author Faue
* @ version 1.0.0
*/
Class ServerMsgSendThread implements Runnable {
Private socket client;
Private writers are out;
Private BufferedReader keyboardInput
private string buffer output string buffer = new string buffer(" Hello ");
/**
* get the input stream of the keyboard
*
* @param s
* @ throw IOException
*/
The public server MsgSendThread (sockets s) throws IOException {
Client = s;;
out = new PrintWriter(client . get output stream(),true);
keyboard input = new buffered reader(new InputStreamReader(system . in));
}
Public invalid operation () {
Try {
And (! client.isClosed()) {
outputStringBuffer.delete(0,outputstringbuffer . length());
outputstringbuffer . append(keyboard input . readline());
out . println(outputstringbuffer . tostring());
}
} catch (IOException e) {
//e . printstacktrace();
System. out.println (client.tostring ()+"Closed!" );
}
}
}
}
Customer:
Realize the connection based on IP address, and create two threads to send and receive information after the connection.
/*
* Date of creation: March 7, 2009
*
*/
Packaging failure. Multi-user;
Import java.io.bufferedreader;
Import java.io.ioexception;
Import java.io.inputstreamreader;
Import java.io.printwriter;
Import java.net.socket;
/**
* customers
*
* @ Author Faue
*/
Public class client {
Private socket mySocket
/**
* Construction method for creating threads
*
* @param IP
* @ throw IOException
*/
The public client (string IP) throws IOException {
Try {
mySocket = new Socket(IP, 10000);
New thread (new servermsgcollectthread (mysocket), "getAndShow"
+ mySocket.getPort())。 start();
New thread (new ClientMsgSendThread(mySocket), "send"
+ mySocket.getPort())。 start();
} catch (IOException e) {
//e . printstacktrace();
System.out.println("Server。 IP:" + IP
+"Port: 10000 cannot be connected");
}
}
Public static void main(String[] args) throws IOException {
Try {
New client (args [0]);
Catch (exception e) {
System.out.println ("Incorrect IP address entered");
}
}
/**
* This class is used to create a thread that receives information sent by the server and displays it.
*
* @ Author Faue
* @ version 1.0.0
*/
Class ServerMsgCollectThread implements Runnable {
Private socket client;
Private BufferedReader in
private string buffer inputstring buffer = new string buffer(" Hello ");
/**
* get the input stream of the socket
*
* @param s
* @ throw IOException
*/
The public server MsgCollectThread (sockets) throws IOException {
Client = s;;
in = new buffered reader(new InputStreamReader(client
. getInputStream()," GBK "));
}
Public invalid operation () {
Try {
And (! client.isClosed()) {
inputStringBuffer.delete(0,inputstringbuffer . length());
inputstringbuffer . append(in . readline());
system . out . println(getMsg(inputstring buffer . tostring()));
}
} catch (IOException e) {
//e . printstacktrace();
System. out.println (client.tostring ()+"Closed!" );
system . exit(0);
}
}
/**
* construct the input string
*
* @param line
* @ Return
*/
Private string getMsg (string line)
Return client.toString()+"means:"+line;
}
}
/**
* This class is used to create a thread to send data.
*
* @ Author Faue
* @ version 1.0.0
*/
ClientMsgSendThread class implements Runnable {
Private socket client;
Private writers are out;
Private BufferedReader keyboardInput
private string buffer output string buffer = new string buffer(" Hello ");
/**
* get the input stream of the keyboard
*
* @param s
* @ throw IOException
*/
Public ClientMsgSendThread (sockets s) throws IOException {
Client = s;;
out = new PrintWriter(client . get output stream(),true);
keyboard input = new buffered reader(new InputStreamReader(system . in));
}
Public invalid operation () {
Try {
And (! client.isClosed()) {
outputStringBuffer.delete(0,outputstringbuffer . length());
outputstringbuffer . append(keyboard input . readline());
out . println(outputstringbuffer . tostring());
}
Out.println("-Bye, bye! - ");
} catch (IOException e) {
//e . printstacktrace();
System. out.println (client.tostring ()+"Closed!" );
system . exit(0);
}
}
}
}
If it helps you, please remember to adopt it as a satisfactory answer, thank you! Wish you a happy life!
Vaella
- Previous article:172 flow card, how was it submitted to the operator for review?
- Next article:ICBC SMS reminds that the game cannot be recharged.
- Related articles
- How to write a farewell speech when leaving the company?
- How to test the application of app software testing in mobile phones?
- What is the abbreviation of ID?
- (100-please enter the master) How to choose stocks? (well said 100)
- Why can't telecom operators send short messages?
- What is the delivery in China?
- Zhejiang writers
- How to write short messages for the classic greetings of the 24 solar terms in beginning of autumn?
- Is there any app software in China Mobile M-Zone?
- How to pick up the ticket after buying it in Gaode Scenic Area?