我试图从我的服务器中输出一个随机数。我有随机数设置并转换为缓冲区读取器的字符串,但我仍然收到错误,任何人都可以看到我出错的地方?
如果有人有兴趣我已经处理了代码,它现在正在工作
更新了工作服务器代码
import java.net.*;
import java.io.*;
import java.util.Random;
public class server extends Thread
{
private ServerSocket serverSocket;
public server(int port) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
}
public void run()
{
System.out.println("Starting game...");
while(true)
{
System.out.println("Client connection established! Game started");
try
{
Socket server = serverSocket.accept();
Random rand = new Random();
int randInt = rand.nextInt(12);
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF("Turning on button " + randInt);
DataInputStream in = new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
out.writeUTF("Acknowledged - Button 1 pressed");
}// End try
catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}// End catch
catch(IOException e)
{
e.printStackTrace();
break;
}// End catch
}// End while()
}// End run()
/*The start of the main loop */
public static void main(String [] args)
{
int port = 4444;
try
{
Thread t = new server(port);
t.start();
}// End try
catch(IOException e)
{
e.printStackTrace();
}// End catch
}// End main()
}/
更新了工作客户代码
import java.net.*;
import java.io.*;
import java.util.Random;
public class client
{
public static void main(String [] args)
{
String serverName = "localhost";
int port = 4444;
try
{
Socket client = new Socket(serverName, port);
Random rand = new Random();
int randInt = rand.nextInt(12);
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Button " + randInt + " pressed");
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
}// End client
catch(IOException e)
{
e.printStackTrace();
}// End catch
}// End main
}
答案 0 :(得分:2)
new InputStreamReader(random)
这是错误。 InputStreamReader
没有InputStreamReader(String)
这样的构造函数。我不确定你想要在这里做什么。否则你可以使用像
PrintWriter outToClient = new PrintWriter(connected.getOutputStream(),true);
out.print(random);
答案 1 :(得分:1)
您的问题出现在服务器类中:
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(random));
InputStreamReader
需要InputSream
作为一个论点,而不是String
。
BTW您的main
方法需要int[]
数组,而不是String[]
。它不会像这样运行!
答案 2 :(得分:0)
读者RETURN字符串,不接受它们,因此在这方面它们是相关的。它们不像你在这里那样工作;我建议你阅读文档,以便将来知道如何使用读者。
无论如何,读者习惯于从某种流中读取文本。你已经有了自己的文字,所以你不需要读者。
在服务器代码的最后,添加outToClient.println(random);
并完全删除BufferedReader
。
while(true)
{
//listens for connection
Socket connected = Server.accept();
//prints clients adddress and port
System.out.println( " THE CLIENT"+" "+ connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED ");
Random rannum = new Random();
int num = rannum.nextInt(12);
String random = Integer.toString(num);
System.out.println("Turning on Button " + num);
PrintWriter outToClient = new PrintWriter(connected.getOutputStream(),true);
outToClient.println(random);
}