这个是网络猜数字的游戏 我做的老有问题哪个高手能帮忙解决下 看看问题出哪了~~谢服务器端
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;public class Fwq { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
ServerSocket server = null;
Socket socket = null;
DataInputStream dis = null;
DataOutputStream doc = null;
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
server = new ServerSocket(1223);
} catch (IOException e) {
System.out.println("启动失败");
}
System.out.println("服务器已经启动,等待连接。。");
try {
socket = server.accept();
} catch (IOException e) {
}
System.out.println("已连接客户端" + socket.getInetAddress().toString());
try {
dis = new DataInputStream(socket.getInputStream());
doc = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
}
int str = 0;
String ts=null;
Random ran = new Random();
int i = (ran.nextInt(100));
int count=0;
System.out.println("随即数"+i);
while (true) {
try {
str = dis.readInt();
} catch (Exception e) {
if(str<i){
ts=("太小");
count++;
}
else if(str>i){
ts=("太大");
count++;
}
else {
ts=("答对");
break;
}
System.out.println("接受数据" + str);
try {
doc.writeUTF(ts);
if(str==i){
break;
}
} catch (IOException e1) {
System.out.println("错误");
}
}
}
}
}
客户端import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;public class Kehuduan {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Socket socket = null;
DataInputStream dis = null;
DataOutputStream doc = null;
BufferedReader sb = new BufferedReader(new InputStreamReader(System.in));
try {
socket = new Socket("192.168.1.60", 1223);
} catch (UnknownHostException e) {
System.out.println("连接失败,找不到主机");
} catch (IOException e) {
}
System.out.println("与服务器连接成功,服务器信息:"
+ socket.getInetAddress().toString());
try {
dis = new DataInputStream(socket.getInputStream());
doc = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
}
abc: while (true) {
for (int i = 1; i <= 20; i++) {
System.out.println("请输入整数:");
try {
doc.writeInt(Integer.parseInt(sb.readLine()));
} catch (NumberFormatException e) {
System.out.println("传输失败,数据格式不对");
} catch (IOException e) {
}
try {
String tmp = dis.readUTF();
System.out.println(tmp);// 重服务器接收信息;
if (tmp.equals("恭喜你猜对了")) {
break abc;
}
} catch (IOException e) {
break abc;
}
}
}
}
}
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;public class Fwq { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
ServerSocket server = null;
Socket socket = null;
DataInputStream dis = null;
DataOutputStream doc = null;
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
server = new ServerSocket(1223);
} catch (IOException e) {
System.out.println("启动失败");
}
System.out.println("服务器已经启动,等待连接。。");
try {
socket = server.accept();
} catch (IOException e) {
}
System.out.println("已连接客户端" + socket.getInetAddress().toString());
try {
dis = new DataInputStream(socket.getInputStream());
doc = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
}
int str = 0;
String ts=null;
Random ran = new Random();
int i = (ran.nextInt(100));
int count=0;
System.out.println("随即数"+i);
while (true) {
try {
str = dis.readInt();
} catch (Exception e) {
if(str<i){
ts=("太小");
count++;
}
else if(str>i){
ts=("太大");
count++;
}
else {
ts=("答对");
break;
}
System.out.println("接受数据" + str);
try {
doc.writeUTF(ts);
if(str==i){
break;
}
} catch (IOException e1) {
System.out.println("错误");
}
}
}
}
}
客户端import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;public class Kehuduan {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Socket socket = null;
DataInputStream dis = null;
DataOutputStream doc = null;
BufferedReader sb = new BufferedReader(new InputStreamReader(System.in));
try {
socket = new Socket("192.168.1.60", 1223);
} catch (UnknownHostException e) {
System.out.println("连接失败,找不到主机");
} catch (IOException e) {
}
System.out.println("与服务器连接成功,服务器信息:"
+ socket.getInetAddress().toString());
try {
dis = new DataInputStream(socket.getInputStream());
doc = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
}
abc: while (true) {
for (int i = 1; i <= 20; i++) {
System.out.println("请输入整数:");
try {
doc.writeInt(Integer.parseInt(sb.readLine()));
} catch (NumberFormatException e) {
System.out.println("传输失败,数据格式不对");
} catch (IOException e) {
}
try {
String tmp = dis.readUTF();
System.out.println(tmp);// 重服务器接收信息;
if (tmp.equals("恭喜你猜对了")) {
break abc;
}
} catch (IOException e) {
break abc;
}
}
}
}
}
最主要就是try {
socket = server.accept();
} catch (IOException e) {
}
System.out.println( "已连接客户端 " + socket.getInetAddress().toString());
try {
dis = new DataInputStream(socket.getInputStream());
doc = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
} 这些代码了 我也没详细看,可能还包括其他代码.不然的话,你收到一次消息后就永远都收不到了,这样就抛出异常咯!~看下书先,相信一个小时就能搞定了,我说的话说好多你未必听懂.
import java.io.*;
import java.net.*;public class Client {
Socket socket; BufferedReader in; PrintWriter out;
boolean flag = true;
public Client() {
try {
socket = new Socket("192.168.2.110", 10000);
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader line = new BufferedReader(new InputStreamReader(
System.in));
out.println(line.readLine());
System.out.println(in.readLine());
line.close();
out.close();
in.close();
socket.close();
} catch (IOException e) {
}
} public static void main(String[] args) {
new Client();
}
}
import java.net.*;
import java.util.Random;
import java.io.*;public class Server1 {
private ServerSocket ss; private Socket socket; private BufferedReader in; private PrintWriter out; public Server1() {
try {
ss = new ServerSocket(10000);
Random ran = new Random();
int i = (ran.nextInt(100));
String ts = null;
System.out.println("随即数 " + i); while (true) {
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
if("答对".equals(ts)){
ran = new Random();
i = (ran.nextInt(100));
System.out.println("随即数 " + i);
}
String line = in.readLine();
System.out.println(line);
int com = Integer.valueOf(line);
if(com==886)
{
out.close();
in.close();
socket.close();
ss.close();
}
if (com < i) {
ts = "太小";
} else if (com > i) {
ts = "太大";
} else {
ts = "答对";
}
out.println(ts);
}
} catch (IOException e) {
}finally{
if(ss!=null)
{
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} public static void main(String[] args) {
new Server1();
}
}