| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 741 人关注过本帖
标题:java.net.SocketException: Connection reset
只看楼主 加入收藏
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
java.net.SocketException: Connection reset
有同样的帖子,也看了各位大神的回复,修改了自己的代码后,发现,还是报这个错,求解释..........
客户端:
package newpackage;

import *;
import *;
import java.util.Scanner;

public class Client extends Thread{
Socket s;
BufferedReader br;
PrintWriter pw;

    public static void main(String[] args) {
        new Client();

    }
    public Client(){
        deal();
        this.start();
    }
    @Override
    public void run(){
      try{
        while(true){
            System.out.println("注册的命令<register@name=用户名/>");
        System.out.println("登陆的命令<login@name=用户名/>");
        System.out.println("发送信息的命令<message@from=用户名@to=用户名@message=message>");
        System.out.println("注销的命令<logout@name=用户名/> ");
        System.out.println("退出的命令  exit ");
            String mes1=writea();
            if(mes1.equals("exit")){
                pw.close();
                br.close();
                s.close();
                break;
            }else if(mes1.equals("1")){
                String getmess=br.readLine();
            System.out.println(getmess);
            }

            pw.println(mes1);
            pw.flush();

          System.out.println(mes1);
            String getmess=br.readLine();
            System.out.println(getmess);
        }
    }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void deal(){
        int port=8882;
        String id="127.0.0.1";
        try{
         s=new Socket(id,port);
        InputStreamReader reader=new InputStreamReader(s.getInputStream());
         br=new BufferedReader(reader);
         pw=new PrintWriter(s.getOutputStream());
        }catch(Exception e){
            e.printStackTrace();
        }

    }
    public String writea(){
        System.out.println("请输入你的命令");
        Scanner sc=new Scanner(System.in);
        String mes1=sc.next();
        return mes1;
    }

}
服务端:
package newpackage;


import
import

import java.util.*;

public class Server {


    public static void main(String[] args) {
        try {
            ServerSocket server=new ServerSocket(8882);
        //    boolean isLogin = false,state = false;
        //    List<String>l=new ArrayList<String>();
     //       HashMap<Integer,String> hm=new HashMap<Integer,String>();
            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;
 //     HashMap<Integer,String> hm;
    public SocketHandle( Socket socket){
        this.socket=socket;
      
    }

    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{
         System.out.println("hello");
        InputStreamReader reader=new InputStreamReader(socket.getInputStream());
        BufferedReader br=new BufferedReader(reader);
        String getmess=br.readLine();
        System.out.println(getmess+"helloa");
        System.out.println(socket.getPort()+"aaahelloa");
        PrintWriter pw=new PrintWriter(socket.getOutputStream());
        writera(pw,getmess);
      //  pw.println("wawa");
     //   pw.flush();
     socket.shutdownInput();
   //  socket.shutdownOutput();
      //   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());
                 pw.close();
                 dos.close();
    }

}
搜索更多相关主题的帖子: void package Socket public 
2012-11-21 16:41
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
收藏
得分:0 
主要是,客户端报这个错............求指点。
2012-11-21 16:47
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
收藏
得分:0 
,求指点...
2012-11-21 16:59
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
收藏
得分:0 
就是,我想让客户端,一直能不断地输入,然后服务器端根据输入的信息进行判断.....
怎么能让客户端不断地输入?
服务器端,应该没问题,不用多看,直接看客户端就行...
2012-11-21 17:03
shellingford
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:19
帖 子:228
专家分:1348
注 册:2010-8-9
收藏
得分:20 
试了一个服务器端不存在的命令,得到一个Software caused connection abort: recv failed
问题在于 客户端采用while(true)循环发命令  但你的服务器端只接收一次,接收完之后立刻调用socket.shutdownInput(); 关闭了输入流


2012-11-22 09:53
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
收藏
得分:0 
回复 5楼 shellingford
呵呵,已经搞好了,谢谢哈。客户端需要再起一个线程用来接受服务器端发送过来的消息,然后服务器端的流都不能关闭,就可以了
2012-11-22 14:15
快速回复:java.net.SocketException: Connection reset
数据加载中...
 
   



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

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