服务器:
package filesharing;
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String args[])throws Exception
{
System.out.println("Server pornit...");
/* Asteapta pe portul 1412 */
ServerSocket server = new ServerSocket(1412);
/* Accepta socketul */
Socket sk = server.accept();
System.out.println("Client acceptat de catre server pe portul: "+server.getLocalPort());
InputStream input = sk.getInputStream();
BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
/* Citeste calea fisierului */
String filename = inReader.readLine();
if ( !filename.equals("") ){
/* Trimite status READY catre client */
outReader.write("READY\n");
outReader.flush();
}
/* Creaza fila noua in directorul tmp */
FileOutputStream wr = new FileOutputStream(new File("C://tmp/" + filename));
byte[] buffer = new byte[sk.getReceiveBufferSize()];
int bytesReceived = 0;
while((bytesReceived = input.read(buffer))>0)
{
/* Scrie in fila */
wr.write(buffer,0,bytesReceived);
}
}
}
客户端:
package filesharing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Client extends JFrame implements ActionListener {
private JTextField txtFile;
public static void main(String args[]){
/* Creare pannel client */
Client clientForm = new Client();
clientForm.Display();
}
public void Display(){
JFrame frame = new JFrame();
frame.setTitle("Client");
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
JLabel lblFile = new JLabel("Fisier:");
txtFile = new JTextField();
txtFile.setPreferredSize(new Dimension(150,30));
JButton btnTransfer = new JButton("Transfer");
btnTransfer.addActionListener(this);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(layout);
mainPanel.add(lblFile);
mainPanel.add(txtFile);
mainPanel.add(btnTransfer);
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
/* Casuta File Open Dialog pentru selectarea fisierului */
JFileChooser fileDlg = new JFileChooser();
fileDlg.showOpenDialog(this);
String filename = fileDlg.getSelectedFile().getAbsolutePath();
txtFile.setText(filename);
try{
/* Incearca conectarea la serverul localhost pe portul 1412 */
Socket sk = new Socket("localhost", 1412);
OutputStream output = sk.getOutputStream();
/* Trimite numele fisierului la server */
OutputStreamWriter outputStream = new OutputStreamWriter(sk.getOutputStream());
outputStream.write(fileDlg.getSelectedFile().getName() + "\n");
outputStream.flush();
/* Asteapta raspunsul de la server */
BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
String serverStatus = inReader.readLine(); // Citeste prima linie
/* Daca serverul e READY trimite fisierul */
if ( serverStatus.equals("READY") ){
FileInputStream file = new FileInputStream(filename);
byte[] buffer = new byte[sk.getSendBufferSize()];
int bytesRead = 0;
while((bytesRead = file.read(buffer))>0)
{
output.write(buffer,0,bytesRead);
}
output.close();
file.close();
sk.close();
JOptionPane.showMessageDialog(this, "Transfer complet");
}
}
catch (Exception ex){
/* Catch pentru eventuale erori */
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
}
答案 0 :(得分:1)
如果您想要处理在单个线程中运行每个服务器请求所需的多个连接,那么您是对的。背后的理论如下......
while (true)
accept a connection ;
create a thread to deal with the client ;
end while
或更多java-alike:
while(true) {
clientSocket = serverSocket.accept();
YourWorkerThread thread = new YourWorkerThread(clientSocket);
thread.start();
}
哪里......
YourWorkerThread
是一个扩展Thread
并实现服务器逻辑所在的方法run
的类。
因此,查看服务器端代码,Socket sk = server.accept();
之后的所有代码行都应该放在扩展run
的类的Thread
方法中。
答案 1 :(得分:0)
您可以使用Runnable
实现接口new Thread(myRunnable).start()
并启动实际工作 - 这是简单版本。确保您的代码是线程安全的,否则会干扰您的其他线程。
答案 2 :(得分:0)
要define and start a thread,您需要传递到Runnable实例的Thread类的实现。
start非常相似,会为您提供一个开始的地方。一个答案指向This question。如果您还没有阅读过Socket教程,那么你肯定应该这样做。
但是,不要仅仅知道线程的基础知识,否则你会遇到问题。并发是一个难题。您将想要了解a socket tutorial that shows how to use multiple clients的伟大世界。
理想情况下,您将获得足够的灵感来阅读Java concurrency,这是Java中并发性的一个惊人参考。