SOCKET处理并发服务器和客户端通信 [英] SOCKET Handling Concurrent server and client comunication
问题描述
我遇到了套接字的一些问题。
I'm experiencing some problems with sockets.
我正在尝试使并发服务器接受多个客户端。
I'm trying to make a concurrent server to accept more than one clients.
当客户端连接到服务器时,服务器会创建一个新线程并侦听套接字。
然后,如果客户端发送了什么,服务器必须读取它。
When a client connects to the server, the server creates a new thread and listens to the socket. Then, if the client sends something, the server must read it.
在客户端上,我只需打开一个字典(txt表单),然后通过套接字发送它。
On the client i just open a dictionary (txt form) and i send it over the socket.
在服务器上我唯一得到的是:
On the server the only thing i get is this:
null
服务器
try
{
String message,file = new String("StressTextFile.txt");
File filee = new File(file);
long length;
// Open the file
FileInputStream fstream = new FileInputStream(file);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = new String();
length = filee.length();
ProgressBar.setMaximum((int)length); //we're going to get this many bytes
ProgressBar.setValue(0); //we've gotten 0 bytes so far
//Read File Line By Line
font1 = new Font("Helvetica", Font.PLAIN, 18);
font1.isBold();
color = new Color( 74,118,110);
TextArea1.setForeground(color);
TextArea1.setFont(font1);
int soma=0;
while(in.readLine() != null)
{
strLine = in.readLine(); // reads from file
//System.out.println(strLine);
TextArea1.append(strLine+"\n");
pwOut.write(strLine);
pwOut.flush();
soma+=strLine.length()+1;
ProgressBar.setValue(ProgressBar.getValue()+(strLine.length()+1)*2);
ProgressBar.repaint();
};
br.close();
pwOut.close();
Skt.close();
}catch (Exception e){ System.err.println("Error: " + e.getMessage()); }
}
客户
try
{
brIn = new BufferedReader(new InputStreamReader(skt.getInputStream()));
while(s != null)
{
s = brIn.readLine();
System.out.println("#####");
System.out.println(s);
}
}
catch (IOException e1) {e1.printStackTrace(); }
请忘记Swing组件。
我认为所有套接字的东西都可以。为什么我不能在服务器端得到任何东西?
Please forget the Swing components. I think all socket stuff are ok. why can't i get nothing on the server side?
请帮助
亲切的问候
推荐答案
多线程服务器的准系统示例:
A barebones example of a multi-threaded server:
//MyServer.java
public class MyServer {
private static int PORT = 12345;
public static void main(String args[]) {
ServerSocket s = new ServerSocket(PORT);
while(true) new MyServerThread(s.accept());
}
您的服务器主题:
//MyServerThread.java
public class MyServerThread implements Runnable {
private InputStream in = null;
private OutputStream out = null;
public MyServerThread(Socket s) {
in = s.getInputStream();
out = s.getOutputStream();
(new Thread(this)).start();
}
public void run() {
//do stuff with **in** and **out** to interact with client
}
}
此示例中缺少:
- 错误处理
-
关闭()
输入套接字/流 - 关闭服务器
- 客户端
- Error handling
close()
ing the sockets/streams- shutting down the server
- the clientside
希望这会给你一些想法通常看起来如何。
Hopefully that gives you some idea of how it usually looks.
这篇关于SOCKET处理并发服务器和客户端通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!