日期:2014-05-20  浏览次数:20804 次

关于JAVA网络编程中的多线程,求指教!
我要完成的功能是这样的:首先开启服务器,等待客户端连接。当客户端连接成功后,就进行数据处理。在这数据处理的同时,服务器还在监听状态,保持和客户端的连接!在这时候要完全的吧接收和发送分开,也就是说,客户端和服务器端都可以随时的进行数据的收发!我已经可以完成接收和发送了,但是还没有很好的去利用线程!请看一下我的具体代码:
开启服务器的类:
public class Start {
  // private StringBuffer receive=null;
public static void main(String[] args) throws IOException{
Server server=new Server();
server.start();//启动服务器
StringBuffer receive=server.getReceive();
System.out.println("接收到的所有值为:"+receive.toString());
}
}
服务器开启,已经数据处理的类:
public class Server {
private ServerSocket serverSocket;
private Socket socket;
private StringBuffer receive;
/**
* 启动服务器
* @throws IOException 
*/
public void start() throws IOException{
System.out.println("启动服务器") ;
serverSocket();//启动监听状态serverSocket()
//while(true){
Socket socket = serverSocket.accept();//得到客户端连接,程序进入到阻塞状态
SSocket ssocket=new SSocket(socket);
new Thread(ssocket).start();//执行线程
//}
}
/**
* 启动监听程序
* @throws IOException 
*/
public void serverSocket() throws IOException{
System.out.println("服务器开始监听......");
this.serverSocket =new ServerSocket(8088) ;// 服务器在8888端口上监听
}
/**
* 得到客户端连接socket()
* @throws IOException 
*/
public void socket() throws IOException{
System.out.println("主程序等待客户端连接......");
this.socket = serverSocket.accept();
}
/**
* 传人接收到的数据存到,返回接收到的数据
*/
public void setReceive(StringBuffer receiv){
this.receive=receiv;
}
/**
* 将接收到的数据返回
*/
public StringBuffer getReceive(){
return this.receive;
}
/**
* 发送数据,返回发送是否成功
* @throws IOException 
 
public boolean sendServer(String send) throws IOException{

return true;
}*/
}
线程类:
/**
 * 线程类
 */
public class SSocket extends Thread{
private StringBuffer receive=new StringBuffer();//接收
Socket socket=null;
public SSocket(Socket socket){
this.socket=socket;

/**
* 定义线程的run方法
*/
public void run(){
System.out.println("线程等待客户端连接......");
Server server=new Server();//创建Server类的对象
try {
PrintWriter output=new PrintWriter(socket.getOutputStream(),true );//输出流,发送数据到客户端
output.flush();//刷新数据
BufferedReader buf = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));//输入流,接收客户端的数据
boolean bContinue =true;
while(bContinue){
String reseiveSTR=buf.readLine();
System.out.println("接收到的值为:"+reseiveSTR);
if(reseiveSTR.equals("exit")){
System.out.println("接收到的所有值为:"+receive.toString());
server.setReceive(receive);//将接收到的数据传入到Server类
buf.close();
output.close();
bContinue =false;
System.out.println("关闭线程");
this.socket.close();
yield(); //向其他线程退让运行权
}
this.receive.append(reseiveSTR);
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



------解决方案--------------------
呵呵正培训java呢。。我今天正好乱写了个。。仅作参考很垃圾的。。
Java code

/*服务器端代码*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class BasicServer extends Thread{
    static ServerSocket ss;
    static Socket s;
    static BufferedWriter out;
    static BufferedReader in;
    static BufferedReader br;
    static boolean f = true;
    public void run(){
        String str;
        while(f){
            try {
                str = br.readLine();
                out.write(str+"\n");
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[]args){
        try {
            ss  = new ServerSocket(8838);
            Socket s = ss.accept();
            out  = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            br = new BufferedReader(new InputStreamReader(System.in));
            new BasicServer().start();
            while(true){
                String str2 = in.readLine();
                System.out.println(InetAddress.getLocalHost()+"说:"+str2);
                if(str2.equals("end"))
                break;
            }
            BasicServer.f = false;
            br.close();
            in.close();
            out.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }
}
/*客户端代码*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

public class BasicClient extends Thread{
    
    static Socket sc;
    static BufferedWriter out;
    static BufferedReader in;
    static BufferedReader br;
    static boolean f = true;
    public void run(){
        String str;
        while(f){
            try {
                str = br.readLine();
                out.write(str+"\n");
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[]args) throws IOException{
        sc = new Socket(InetAddress.getLocalHost(),8838);
        in = new BufferedReader(new InputStreamReader(sc.getInputStream()));
        out = new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
        br = new BufferedReader(new InputStreamReader(System.in));
        new BasicClient().start();
        while(true){
            String str2 = in.readLine();
            System.out.println(InetAddress.getLocalHost()+"说:"+str2);
            if(str2.equals("end"))
            break;
        }
        
        BasicClient.f = false;
        out.close();
        in.close();
        br.close();
        sc.close();
    }
}
我也新学java的。。可以来新建群里共同学习交流208779755