| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 459 人关注过本帖
标题:TCP写的一个点对点的聊天程序练习,可是总是有问题,连接上不能输入,求大神 ...
只看楼主 加入收藏
coolnanlove
Rank: 1
等 级:新手上路
帖 子:17
专家分:7
注 册:2011-12-5
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:3 
TCP写的一个点对点的聊天程序练习,可是总是有问题,连接上不能输入,求大神帮助
程序代码:
/**

 * @author 小南

 * 客户端

 */
import *;
import *;
public class TCPClient {
    public static void main(String args[]) throws Exception{
        Socket s = new Socket("172.0.0.1",5003);
         System.out.println("client connect is ok");
        Thread.sleep(2000);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader netin = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter netou = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String rd = null;
        rd = br.readLine();
        while(rd!="bye") {
            netou.write(rd);
            netou.flush();
            System.out.println("Server:" + netin.readLine());
            rd = br.readLine();
        }
        br.close();
        netin.close();
        netou.close();
        s.close();
    }
}
这里是服务器端
程序代码:
/**

 * @author 小南

 * 服务器端

 */
import *;
import *;
public class TCPServer {
    public static void main(String args[]) throws Exception{
        ServerSocket ss= new ServerSocket(5003);
        Socket s = new Socket();
        s = ss.accept();
        Thread.sleep(2000);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader netin = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter netou = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String rd = null;
        rd = br.readLine();
        while(rd!="bye") {
            netou.write(rd);
            netou.flush();
            System.out.println("Client:" + netin.readLine());
            rd = br.readLine();
        }
        br.close();
        netin.close();
        netou.close();
        s.close();
        ss.close();
       
    }
}

连接正常,但是不能通话,实在找不到问题,求大神解决
搜索更多相关主题的帖子: 客户端 Socket 点对点 color 
2012-11-26 19:52
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
收藏
得分:20 
本人前几天刚写了一个,虽说不是你要的那种聊天,发出来,让你参考一下。。
客户端:
package newpackage;
import *;
import *;
import java.util.Scanner;
public class Client {
    BufferedReader in = null;
    PrintWriter out = null;
    Socket socket = null;
    String message = null;
    Scanner cin = new Scanner(System.in);
    public static void main(String[] args) throws Exception, IOException {
        new Client(new Socket("127.0.0.1", 8880));

    }
    public Client(Socket socket) {
        this.socket = socket;
        try {
            in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // init receiver thread
        Receiver receiver = new Receiver();
        Thread tReceiver = new Thread(receiver);
        tReceiver.start();
        // init sender thread
        Send send = new Send();
        Thread tsend = new Thread(send);
        tsend.start();

    }
    public void printMenu() {
    System.out.println("注册的命令<register@name=用户名/>");
        System.out.println("登陆的命令<login@name=用户名/>");
        System.out.println("发送信息的命令<message@from=用户名@to=用户名@message=message>");
        System.out.println("注销的命令<logout@name=用户名/> ");
    }
    private class Send implements Runnable {
        @Override
        public void run() {
            printMenu();
            while (cin.hasNext()) {
            String    mes = cin.next();
            
                out.println(mes);
                out.flush();
               System.out.print(mes);
            }
        }

    }
    private class Receiver implements Runnable {
        @Override
        public void run() {
            try {
                while (true) {
                    message = in.readLine();
                    System.out.println(message);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
服务器端:
package newpackage;

import
import
import java.util.*;
public class Server {

    public static void main(String[] args) {
        try {
            ServerSocket server=new ServerSocket(8880);
            while(true){
                Socket socket=server.accept();
               System.out.println("hello");
                SocketHandle handle=new    SocketHandle(socket);
                Thread thread=new Thread(handle);
                thread.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package newpackage;
import ch3.*;
import *;
import
import java.util.*;
public class SocketHandle implements Runnable {
     static  boolean isLogin,state;
    static List<String>l=new ArrayList<String>();
   static HashMap cl=new HashMap();
    private Socket socket;
    BufferedReader br;
    PrintWriter pw;
 //     HashMap<Integer,String> hm;
    public SocketHandle( Socket socket){
        this.socket=socket;
        try{
         br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
         pw=new PrintWriter(socket.getOutputStream());
    }catch(Exception e){
        e.printStackTrace();
    }
    }
    public void writera(PrintWriter pw,String getmess)throws Exception{
        if(getmess.contains("register")){
            int t=getmess.indexOf("=");
            String getm=getmess.substring(t+1,getmess.length()-2);
            if(l.contains(getm)){
                pw.println("<result command=”register” state=”error” message=”” />");
                pw.flush();
            }else{
                l.add(getm);
        
            System.out.println(getm);
                pw.println(" <result command=”register” state=”ok” />");
                pw.flush();
            }
        }else if(getmess.contains("login")){
            System.out.println("login");
            int t=getmess.indexOf("=");
            String getm=getmess.substring(t+1,getmess.length()-2);
            System.out.println(getm+"dfdfdfddfdf"+getm.equals(l.get(0)));
            if(!state&&isOr(getm)){
                pw.println("<result command=” login ” state=”ok” />");
                 pw.flush();
                     isLogin=true;
                      try{
                            cl.put(getm, socket);
                            }catch(Exception e){
                                e.printStackTrace();
                            }
            }
            else {
                pw.println("<result command=”login” state=”error” message=”” />;");
                 pw.flush();
            }
        }else if(getmess.contains("message")){
            if(isLogin){
                            int i1=getmess.indexOf("to");
                  int i2=getmess.lastIndexOf("@");
                  String str=getmess.substring(i1+3, i2);
                  System.out.println("hhhhhhhhhh"+str);
                 if(cl.containsKey(str)){
                    pw.println("<result command=” message ” state=”ok” />;");
                      pw.flush();
                 sendmessage(str,getmess);
                 }else{
                         pw.println("你要发送的用户没有注册");
                      pw.flush();
                 }
            
               //  hm.put(socket.getPort(), getmess);
            }else{
                pw.println("请登录。。。。。。");
                 pw.flush();
            }
        }else if(getmess.contains("logout")){
            if(!state){
                int t=getmess.indexOf("=");
            String getm=getmess.substring(t+1,getmess.length()-2);
                pw.println("<result command=” logout ” state=”ok” />");
                 pw.flush();
                l.remove(getm);
            }
            else {
                pw.println("<result command=”loginout” state=”error” message=”” />");
                 pw.flush();
            }
        }else{
            pw.println("输入信息有误");
                 pw.flush();
        }
    }
  public boolean isOr(String mess){
    for(int j=0;j<l.size();j++){
        if(mess.equals(l.get(j)))return true;
    }
    return false;
  }
    @Override
    public void run() {
    try{
         while(!state){
        String getmess=br.readLine();
   
        writera(pw,getmess);
         }
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
        pw.close();
         br.close();
        socket.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    }
    private void sendmessage(String name ,String mess)throws Exception {
        Socket dos=(Socket)cl.get(name);
            PrintWriter pw=new PrintWriter(dos.getOutputStream());
                  pw.println(mess);
                 pw.flush();
                 System.out.println("怎么了,");
                  System.out.println(cl.size());
    }
}
2012-11-27 10:51
coolnanlove
Rank: 1
等 级:新手上路
帖 子:17
专家分:7
注 册:2011-12-5
收藏
得分:0 
回复 2楼 whhguo
我想问一下,这个发送数据必须要用PrintWriter做通道么?用BufferedWriter里面的Write做发送不行么?我看你们都是用PrintWriter的
2012-11-27 14:05
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
收藏
得分:0 
行的。。。。。
2012-11-27 14:40
快速回复:TCP写的一个点对点的聊天程序练习,可是总是有问题,连接上不能输入, ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055331 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved