首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java网络编程

java网络编程
EN

Stack Overflow用户
提问于 2011-11-28 12:26:24
回答 1查看 240关注 0票数 0

我有两个类,一个是服务器,另一个是客户端,客户端程序接受一个整数写到服务器,然后创建一个游戏对象,然后返回一个数字给客户端,如果返回的数字是0,则游戏停止,否则继续输入整数。下面是代码,问题是,服务器程序只接受1次,然后客户端就会抛出一个异常,我认为应该是服务器程序的编码有问题。

server.java

代码语言:javascript
复制
import java.io.*;
import java.net.*;

class  ClientHandler extends Thread {

    Socket socket;
    Game g;

    public ClientHandler(Socket socket) {
        this.socket = socket;
        g = new Game(50);

    }

    public void run() {
        try {
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

            int x = dis.readInt();     


            int reply = this.g.guess(x);
            System.out.println(x);
            dos.writeInt(reply);

            //System.out.println(reply);
            //System.out.println(reply);

            socket.close();
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }

}
public class Server {
    public static void main(String arg[]) {
        try {
            ServerSocket ss = new ServerSocket(12345);

                        while(true) {
            Socket s = ss.accept();
            ClientHandler ch = new ClientHandler(s);
                        ch.start();

                        }
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

client.java

代码语言:javascript
复制
import java.util.Scanner;
import java.net.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Client {

   public static void main(String args[]) {
      Socket socket = null;
      DataInputStream in = null;
      DataOutputStream out = null;

      Scanner console = new Scanner(System.in);
      int guess;
      int result;

      try {
         socket = new Socket("127.0.0.1", 12345);
         in = new DataInputStream(socket.getInputStream());
         out = new DataOutputStream(socket.getOutputStream());
         do {
            System.out.print("Guess a number: ");
            guess = console.nextInt();
            out.writeInt(guess);
            result = in.readInt();
            if (result == 0) {
               System.out.println("Correct!");
            } else if (result == -1) {
               System.out.println("Too small!");
            } else if (result == 1) {
               System.out.println("Too large!");
            } else {
               System.out.println("Too many attempts!");
            }
         } while (result != 0 && result != -2);
      } catch (IOException ioe) {
         ioe.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         try {
            if (in != null) {
               in.close();
            }
            if (out != null) {
               out.close();
            }
            if (socket != null) {
               socket.close();
            }
         } catch (IOException ioe) {
            ioe.printStackTrace();
         }
      }
   }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-28 12:40:08

您可能需要在Client类的main方法中为每次迭代实例化套接字和获取流(输入和输出)。

代码语言:javascript
复制
do {
     socket = new Socket("127.0.0.1", 12345);
     in = new DataInputStream(socket.getInputStream());
     out = new DataOutputStream(socket.getOutputStream());

        System.out.print("Guess a number: ");
      ........
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8291525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档