Home > other >  The socket one-to-many file transfer would like to ask how to solve
The socket one-to-many file transfer would like to ask how to solve

Time:10-04

I have a one to one instance of file transmission, one-to-many message is sent, but what I want is a one-to-many file transfer, hope god can help me to integrate the two pieces of code, help me to achieve the result that I want, small white, a great god directly to the code, genuflect is begged
A, one-to-many message received sent
Accept multiple clients:
Package myjob1;

Import the Java. IO. IOException;
The import java.net.InetAddress;
The import java.net.ServerSocket;
The import java.net.Socket;

/*
* the Socket communication based on TCP protocol, realize the user login
* the server
*/
Public class Server {
Public static void main (String [] args) {
Try {
//1. Create a server Socket and ServerSocket, specify binding port, and to monitor the port
@ SuppressWarnings (" resource ")
ServerSocket ServerSocket=new ServerSocket (99);
The Socket Socket=null;
//record client number
Int count=0;
System. The out. Println (" * * * server is about to start, waiting for the client connection * * * ");
//cycle to monitor waiting for the client connection
While (true) {
//call the accept () method to start listening, waiting for the client connection
The socket=serverSocket. The accept ();
//create a new thread
ServerThread ServerThread=new ServerThread (socket);
//start the thread
ServerThread. Start ();

count++;//statistical client number
System. The out. Println (" number of clients: "+ count);
InetAddress address=socket. GetInetAddress ();
System. The out. Println (" is the current client IP: "+ address. GetHostAddress ());
}
} the catch (IOException e) {
e.printStackTrace();
}
}
}
Client:
Package myjob1;

Import the Java. IO. BufferedReader;
Import the Java. IO. IOException;
Import the Java. IO. InputStream;
Import the Java. IO. InputStreamReader;
Import the Java. IO. OutputStream;
Import the Java. IO. PrintWriter;
The import java.net.Socket;
The import java.net.UnknownHostException;

/*
* the client
*/
Public class Client {
Public static void main (String [] args) {
Try {
//1. Create the client Socket, specify the server address and port
The Socket Socket=new Socket (" localhost ", 99);
//2. Get the output stream, send information to the server side
OutputStream OS=socket. GetOutputStream ();//output byte stream
PrintWriter pw=new PrintWriter (OS);//the output flow packaging printing flow
Pw. Write (" user name: Alice; Password: 789 ");
Pw. Flush ();
Socket. ShutdownOutput ();//close the output stream
//3. Get the input stream, and read the server response information
InputStream is=socket. GetInputStream ();
BufferedReader br=new BufferedReader (new InputStreamReader (is));
String info=null;
While ((info=br. ReadLine ())!=null) {
System. The out. Println (" I am a client, server, said: "+ info).
}
//4. Close the resources
Br. The close ();
Is the close ();
Pw. The close ();
OS. The close ();
Socket. The close ();
} the catch (UnknownHostException e) {
e.printStackTrace();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
Two, one to one file transfer:
Server:
Package myjob2;

Import the Java. IO. BufferedInputStream;
Import the Java. IO. A DataInputStream;
Import the Java. IO. DataOutputStream;
Import the Java. IO. The File;
Import the Java. IO. FileInputStream;
The import java.net.ServerSocket;
The import java.net.Socket;
//transmitted choose file
//public Socket the accept () throws
//IOException listen to and accept the connection to the socket, this method before the connection has been blocked,
//the file name and length to the client, it must be true for all platforms, such as the processing of Chinese name, also need to processing, specific can see Think In Java
//in the 4 th ready-made code,
//read from the input stream contains a certain number of bytes, and store them in the buffer array b
//to integer returns the actual number of bytes to read. Detected in the input data is available, the end of the file (the end of the file)
//or before throwing an exception, this method will block,
//oh, pay attention to close the socket link or the client will wait for the server data,
//until the socket timeout, which leads to the incomplete data,
Public class ServerTest {
Int port=8821;
Private ServerSocket ss;

Void the start () {
The Socket s=null;
Try {
Ss=new ServerSocket (port);
While (true) {
//transmitted choose file
String filePath="D: \ \ lib. Rar";
The File fi=new File (filePath);

System. The out. Println (" file length: "+ (int) fi. Length ());

//public Socket the accept () throws
//IOException listen to and accept the connection to the socket, this method before the connection has been blocked,

S=ss. The accept ();
System. The out. Println (" links to establish a socket ");
A DataInputStream dis=new a DataInputStream (
New BufferedInputStream (s.g etInputStream ()));
Dis. ReadByte ();

A DataInputStream fis=new a DataInputStream (
New BufferedInputStream (new FileInputStream (filePath)));
DataOutputStream ps=new DataOutputStream (s.g etOutputStream ());
//the file name and length to the client, it must be true for all platforms, such as the processing of Chinese name, also need to processing, specific can see Think In Java
//in the 4 th ready-made code,
Ps. WriteUTF (fi) getName ());
Ps. Flush ();
Ps. WriteLong ((long) fi. Length ());
Ps. Flush ();

Int bufferSize=8192;
Byte [] buf=new byte [bufferSize];

While (true) {
Int read=0;
If (fis!=null) {
Read=fis. Read (buf);
//read from the input stream contains a certain number of bytes, and store them in the buffer array b
//to integer returns the actual number of bytes to read. Detected in the input data is available, the end of the file (the end of the file)
//or before throwing an exception, this method will block,
}

If (read==1) {
break;
}
Ps. Write (buf, 0, read);
}
Ps. Flush ();
//oh, pay attention to close the socket link or the client will wait for the server data,
//until the socket timeout, which leads to the incomplete data,
Fis. Close ();
S.c lose ();
System. The out. Println (" file transfer completed ");
}

} the catch (Exception e) {
e.printStackTrace();
}
}

Public static void main (String arg []) {
New ServerTest (). The start ();
}
}
Client:
Package myjob2;

Import the Java. IO. BufferedOutputStream;
Import the Java. IO. A DataInputStream; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related