这是我的服务器代码:
package ServerSideHammingCodeCheckingAndResponse;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
private ServerSocket serverSocket;
private int port;
public Server(int port)
{
this.port = port;
}
public void start() throws IOException
{
System.out.println("Server starts at port:" + port);
serverSocket = new ServerSocket(port);
System.out.println("Waiting for client...");
Socket client = serverSocket.accept();
sendMessage(client, "This is Hamming Code Checking.");
boolean checkInput = false;
String input = null;
while (!checkInput)
{
input = getMessage(client);
if(input.length() == 7 && input.matches("[01]+"))
checkInput = true;
else
sendMessage(client, "invalid");
}
sendMessage(client, input);
}
private void sendMessage(Socket client, String message) throws IOException
{
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
writer.write(message);
writer.flush();
writer.close();
}
private String getMessage(Socket client) throws IOException
{
String userInput;
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
userInput = reader.readLine();
return userInput;
}
public static void main(String[] args)
{
int portNumber = 9987;
try {
Server socketServer = new Server(portNumber);
socketServer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我的客户代码:
package ClientSideDataTransmitter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client
{
private String hostname;
private int port;
Socket socketClient;
public Client(String hostname, int port)
{
this.hostname = hostname;
this.port = port;
}
public void connect() throws UnknownHostException, IOException
{
System.out.println("Attempting to connect to " + hostname + ":" + port);
socketClient = new Socket(hostname, port);
System.out.println("\nConnection Established.");
}
public void readResponse() throws IOException
{
String userInput;
BufferedReader reader = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
System.out.print("Response from server: ");
while ((userInput = reader.readLine()) != null) {
System.out.println(userInput);
}
}
public void sendData() throws IOException
{
Scanner sc = new Scanner(System.in);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socketClient.getOutputStream()));
System.out.println("Enter a 7-bits binary as message to server:\n");
String input = sc.nextLine();
writer.write(input);
writer.flush();
}
public static void main(String arg[])
{
Client client = new Client ("localhost", 9987);
try {
client.connect();
client.readResponse();
client.sendData();
client.readResponse();
} catch (UnknownHostException e) {
System.err.println("Host unknown. Cannot establish connection");
} catch (IOException e) {
System.err.println("Cannot establish connection. Server may not be up." + e.getMessage());
}
}
}
我还没有完成代码,所以请忽略代码中的小错误。
当我启动Server,然后启动Client,并从Client向Server发送输入。服务器似乎没有从客户端获取数据,因为我将该输入从服务器发送回客户端以将其打印出来,它什么都不打印。
我认为服务器代码中的方法getMessage()可能存在问题,但我无法修复它。请帮我修复代码。非常感谢!
服务器:
Server starting at port 9987
Waiting for client...
客户端:
Attempting to connect to localhost:9987
Connection Established.
Response from server: This is Hamming Code Checking.
Enter a 7-bits binary as message to server:
1234567
Response from server:
答案 0 :(得分:0)
代码问题writer.close()
sendMessage()
将在内部关闭编写器对象。您的服务器程序将被关闭,不再连接以接受连接。
答案 1 :(得分:0)
除了Shriram建议之外,我建议您在此处使用PrintWriter以避免关闭连接。它使用起来也更方便一些。这是工作示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket serverSocket;
private int port;
public Server(int port) {
this.port = port;
}
public void start() throws IOException {
System.out.println("Server starts at port:" + port);
serverSocket = new ServerSocket(port);
System.out.println("Waiting for client...");
Socket client = serverSocket.accept();
sendMessage(client, "This is Hamming Code Checking.");
boolean checkInput = false;
String input = null;
while (!checkInput) {
input = getMessage(client);
if (input.length() == 7 && input.matches("[01]+"))
checkInput = true;
else
sendMessage(client, "invalid");
}
sendMessage(client, input);
}
private void sendMessage(Socket client, String message) throws IOException {
PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
writer.println(message);
}
private String getMessage(Socket client) throws IOException {
String userInput;
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
userInput = reader.readLine();
return userInput;
}
public static void main(String[] args) {
int portNumber = 9987;
try {
Server socketServer = new Server(portNumber);
socketServer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private String hostname;
private int port;
Socket socketClient;
public Client(String hostname, int port) {
this.hostname = hostname;
this.port = port;
}
public void connect() throws UnknownHostException, IOException {
System.out.println("Attempting to connect to " + hostname + ":" + port);
socketClient = new Socket(hostname, port);
System.out.println("\nConnection Established.");
}
public void readResponse() throws IOException {
String userInput;
BufferedReader reader = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
System.out.print("Response from server: ");
userInput = reader.readLine();
System.out.println(userInput);
}
public void sendData() throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter writer = new PrintWriter(socketClient.getOutputStream(), true);
System.out.println("Enter a 7-bits binary as message to server:\n");
String input = sc.nextLine();
writer.println(input);
}
public static void main(String arg[]) {
Client client = new Client("localhost", 9987);
try {
client.connect();
client.readResponse();
client.sendData();
client.readResponse();
} catch (UnknownHostException e) {
System.err.println("Host unknown. Cannot establish connection");
} catch (IOException e) {
System.err.println("Cannot establish connection. Server may not be up." + e.getMessage());
}
}
}
答案 2 :(得分:0)
您通过为每条消息创建新的BufferedReader
来丢失数据。您需要在套接字的生命周期中使用相同的一个。同上'PrintWriter`或您用来发送的任何内容。