Serverpool类
public class PooledWebServer implements Runnable{
int port;
ServerSocket Serversocket;
Thread runningThread = null;
boolean isStoped = false;
ExecutorService threadPool = Executors.newFixedThreadPool(10);
public PooledWebServer(int port)
{
this.port = port;
}
@Override
public void run() {
synchronized(this){
this.runningThread = Thread.currentThread();
}
OpenServerSocket();
while(!isStoped()){
Socket clientSocket = null;
try {
clientSocket = Serversocket.accept();
} catch (IOException ex) {
Logger.getLogger(PooledWebServer.class.getName()).log(Level.SEVERE, null, ex);
}
this.threadPool.execute(new WorkerRunnable(clientSocket,"Thread Pool Server"));
}
this.threadPool.shutdown();
System.out.println("Server Stopped!");
}
private void OpenServerSocket(){
try {
Serversocket = new ServerSocket();
Serversocket.bind(new InetSocketAddress("192.168.1.4",port));
System.out.println("Server Started at 192.168.1.4");
} catch (IOException ex) {
Logger.getLogger(PooledWebServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
private synchronized boolean isStoped(){
return this.isStoped;
}
public synchronized void stop()
{
this.isStoped = true;
try{
this.Serversocket.close();
}catch(IOException e){
}
}
}
WorkerThread类
public WorkerRunnable(Socket client,String serverText){
clientSocket = client;
this.serverText = serverText;
}
@Override
public void run() {
try{
BufferedReader input =new BufferedReader( new InputStreamReader(clientSocket.getInputStream()));
//OutputStream out = clientSocket.getOutputStream();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
out.write(("WorkerThread started for client for address: "+clientSocket.getInetAddress()));
// above message is sent to client on connection
String s = input.readLine();
System.out.println("Client "+clientSocket.getInetAddress() +" says: "+s);
out.close();
input.close();
}catch(IOException e){
System.out.println(e.getMessage());
}
}
}
客户
public class Client extends JFrame implements ActionListener{
JPanel pane;
JTextField message;
JButton send;
File myFile;
InputStream input;
OutputStream out;
JLabel server;
Socket client = null;
public Client()
{
pane = new JPanel();
server = new JLabel("Send to connect!");
message = new JTextField();
send = new JButton("Send");
send.addActionListener(this);
//connect();
pane.setLayout(new GridLayout(3,1));
pane.add(server);
pane.add(message);
pane.add(send);
this.add(pane);
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == send){
sendMessage();
}
}
void connect(){
client = new Socket();
try {
client.connect(new InetSocketAddress("192.168.1.4",9000));
System.out.println("Client connected to: "+client.getLocalAddress());
server.setText("Client connected to: "+client.getLocalAddress().toString());
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
void sendMessage() {
try {
connect();
BufferedReader inp =new BufferedReader( new InputStreamReader(client.getInputStream()));
String s = inp.readLine(); // Program hangs here......
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
String m = message.getText().trim();
if(myFile == null){
output.write(m);
}else{
output.write(m);
}
output.close();
inp.close();
} catch (Exception ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
client.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
它有什么问题?我是java中的套接字的新手。我想向服务器发送消息和从服务器发送消息。
这里是服务器的输出:out.write(("WorkerThread started for client for address: "+clientSocket.getInetAddress()));
客户不读。
当我使用浏览器连接时,我收到消息!
我的代码是对的,还是我错过了什么?