java socket问题,来帮忙!!!

daishunchao 发布于 2011/04/16 16:05
阅读 598
收藏 1

服务端

import java.io.*;
import java.net.*;
public class Server {
public static void main(String args[]) {
    ServerSocket ss=null;
    Socket socket=null;
    boolean listening=true;
    try {
        ss=new ServerSocket(1005);
        int n=0;
        System.out.println("端口号是:"+ss.getLocalPort());
        while(listening) {
            new ServerThread(ss.accept(),n).start();
            n++;
        }
        socket.close();

    } catch(Exception e)
    {

    }

    }
}
class ServerThread extends Thread {
    Socket socket=null;
    int clientNum;
    BufferedReader br=null;
    BufferedReader br2=null;
    InputStream is=null;
    OutputStream os=null;
 //   PrintWriter pw=null;
    DataOutputStream dos=null;
    boolean flag=true;
    String word=null;
    String word2=null;
    public ServerThread(Socket socket,int num) {
        this.socket=socket;
        this.clientNum=num+1;
    }
    public void run() {
        try {          
                 is=socket.getInputStream();
                 os=socket.getOutputStream();
                 br=new BufferedReader(new InputStreamReader(is));
                 br2=new BufferedReader(new InputStreamReader(System.in));
                 //pw=new PrintWriter(os);
                 dos=new DataOutputStream(os);
                 while((word=br.readLine())!=null) {
                System.out.println("客户端说:"+word);
            }
                 while((word2=br2.readLine())!=null) {
                 dos.writeUTF(word2);
                 dos.flush();
            }                                                    
        } catch(Exception e) {
            System.out.println("失败了");
        }
    }
}

客户端:

import java.io.*;
import java.net.*;
public class Client implements Runnable{
    Socket socket=null;
    int clientNum;
    BufferedReader br=null;
    BufferedReader br2=null;
    InputStream is=null;
    OutputStream os=null;
    PrintWriter pw=null;
    DataOutputStream dos=null;
    boolean flag=true;
    String word=null;
    String word2=null;
    Thread thread=null;
    public void start() {
        try {
            socket=new Socket("127.0.0.1",1005);
            System.out.println("连接到了服务器");    
            thread=new Thread(this);
            thread.start();
           

        } catch(Exception e) {

        }
    }
    public void run() {
        try
        {
        is=socket.getInputStream();
        os=socket.getOutputStream();
        br=new BufferedReader(new InputStreamReader(is));
        dos=new DataOutputStream(os);
        while((word=br.readLine())!=null) {
        System.out.println("服务端说:"+word);
            }
        while((word2=br2.readLine())!=null) {
        dos.writeUTF(word2);
        dos.flush();
            }

        } catch(Exception e){}
    }
    public static void main(String args[]) {
        new Client().start();
    }
}
互相不能发送怎么回事??帮忙看下好不???

加载中
0
jingshishengxu
jingshishengxu

客户端,服务器都先read,能互相发送吗?(就像两个人见面都在等对方先说话)

0
jingshishengxu
jingshishengxu

java用BIO的话,服务器端建议N+1模式,N个读线程,一个写线程

0
一号男嘉宾
一号男嘉宾

何不试试apache的MINA

0
张林
张林

引用来自#4楼“幽幽”的帖子

何不试试apache的MINA

能详细说说吗,怎么用,我都没用过

0
Jwindy
Jwindy

数据通讯的顺序有错,发生死锁,双方都在读对方的输入信息,没有写的信息。通常都是一个写先,一个读先。

Jwindy
Jwindy
读和写分别创建一个线程来操作
返回顶部
顶部