首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

socket 多客户和服务端的连接怎样实现? 小弟我要怎样在代码里来一个客户创建一个线程?请高手指高分回报!多谢

2012-02-02 
socket 多客户和服务端的连接怎样实现? 我要怎样在代码里来一个客户创建一个线程?请高手指高分回报!!谢谢!

socket 多客户和服务端的连接怎样实现? 我要怎样在代码里来一个客户创建一个线程?请高手指高分回报!!谢谢!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
public class TalkServer {
public static void main(String[] args) {
try {
ServerSocket server = null;
//boolean listening=true;
try {
server = new ServerSocket(14700);

} catch (IOException e) {
System.out.println("can't listen to:" + e);
e.getMessage();

while(true){
Socket socket = null;
try {
socket = server.accept(); //接收客户连接
Thread workThread=new Thread (); //创建一个工作线程
workThread.start(); //启动工作线程
} catch (Exception e) {
System.out.println("error " + e);
e.printStackTrace();
}

String line;
BufferedReader is = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("client:" + is.readLine());
line = sin.readLine();
while (!line.equals("bye")) {
os.println(line);
os.flush();
System.out.println("client:" + is.readLine());
line = sin.readLine();
}

is.close();
os.close();
socket.close();
server.close();
}
} catch (Exception e) {
System.out.println("error:" + e);
e.getMessage();
}

}

}


[解决办法]
以前在网上抄的一个例子:
PooledConnectionHandler.java
import java.io.*;
import java.net.*;
import java.util.*;

public class PooledConnectionHandler implements Runnable 
{
protected Socket connection;
protected static List pool = new LinkedList();
public PooledConnectionHandler() {}

public void handleConnection() {
try {
PrintWriter streamWriter = new PrintWriter(connection.getOutputStream()); 
BufferedReader streamReader = new BufferedReader(new 
InputStreamReader(connection.getInputStream())); 
/*String fileToRead = streamReader.readLine();
BufferedReader fileReader = new BufferedReader(new FileReader(fileToRead));
String line = null;
while((line=fileReader.readLine())!=null)
streamWriter.println(line); 
fileReader.close();*/
String line = streamReader.readLine();
if(line.indexOf("cogent") != -1)
streamWriter.println("success");
else
streamWriter.println("failure");
streamWriter.close();
streamReader.close();
}
catch(FileNotFoundException e) {
System.out.println("");
}
catch(IOException e) {
System.out.println(""+e);
}
}
public static void processRequest(Socket requestToHandle) {
synchronized(pool) {
pool.add(pool.size(), requestToHandle);
pool.notifyAll();
}
}
public void run() {
while(true) {
synchronized(pool) {
while(pool.isEmpty()) {
try {
pool.wait();
}
catch(InterruptedException e) {
e.printStackTrace(); 
}
}
connection = (Socket)pool.remove(0); 


}
handleConnection();
}
}
}

PooledRemoteServer.java

import java.io.*;
import java.net.*;

public class PooledRemoteServer 
{
protected int maxConnections;
protected int listenPort;
protected ServerSocket serverSocket;

public PooledRemoteServer(int aListenPort, int maxConnections) 
{
listenPort= aListenPort;
this.maxConnections = maxConnections;
}

public void acceptConnections() 
{
try {
ServerSocket server = new ServerSocket(listenPort, 5);
Socket incomingConnection = null;
while(true) {
incomingConnection = server.accept();
handleConnection(incomingConnection);
}
}
catch(BindException e) {
System.out.println("");
}
catch(IOException e) {
System.out.println(""+listenPort);
}
}

protected void handleConnection(Socket connectionToHandle) {
PooledConnectionHandler.processRequest(connectionToHandle);
}

public void setUpHandlers() 
{
for(int i=0; i<maxConnections; i++) {
PooledConnectionHandler currentHandler = new PooledConnectionHandler();
new Thread(currentHandler, "Handler " + i).start();
}
}

public static void main(String args[]) 
{
PooledRemoteServer server = new PooledRemoteServer(25000, 3);
server.setUpHandlers(); 
server.acceptConnections();
}
}

热点排行