服务器不显示来自客户端的消息(JAVA)
Server not displaying messages from client(JAVA)
我正在发送消息:
newbie:hi
文本出现在服务器控制台上=>服务器收到它但是当我回显时,为了将相同的消息打印到我的文本客户端控制台它没有显示这是我的回显代码及以下您还可以从 TextClient 中查看代码:(我做错了什么?它通常应该显示我从 TextClient 发送的消息)方法 getAll() returns 一个包含消息和发件人的字符串。
public void serverEcho() {
try {
ObjectInputStream objectin = new ObjectInputStream(_Socket
.getInputStream());
Message newmess = (Message) objectin.readObject();
System.out.println(newmess.getAll());
} catch (IOException e) {e.printStackTrace();
} catch (ClassNotFoundException e) {e.printStackTrace();
}
}
这是 TextClient:
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class TextClient {
public TextClient() {
}
public static void main(String[] args) throws IOException,
UnknownHostException, ConnectException, InterruptedException {
Socket _socket = new Socket("localhost", 9000);
System.out.println("Please enter your desired username:");
Scanner _new = new Scanner(System.in);
String _inputname = _new.nextLine();
System.out.println("Success.You can now type your messages!");
ClientPeer _clientpeer = new ClientPeer(_inputname, _socket);
Scanner _scan = new Scanner(System.in);
String _input = _scan.nextLine();
while (true) {
if (!_input.equals("exit")) {
if (_input.startsWith("/w"))
_clientpeer.sendMessage(_input, "username");
else {
_clientpeer.sendMessage(_input);
_clientpeer.serverEcho();
}
} else
break;
_input = _scan.nextLine();
}
}
}
ClientPeer class:
import java.net.*;
import java.io.*;
public class ClientPeer{
String _username;
static Socket _Socket;
public ClientPeer(String _username, Socket _Socket)
{
this._username = _username;
this._Socket = _Socket;
}
public void sendMessage(String _message) throws IOException {
ObjectOutputStream _obj = new ObjectOutputStream(
_Socket.getOutputStream());
_obj.writeObject(new Message(_username, _message));
_obj.flush();
}
public void sendMessage(String _message, String _receiver)
throws IOException {
ObjectOutputStream _obj = new ObjectOutputStream(
_Socket.getOutputStream());
_obj.writeObject(new PrivateMessage(_username, _message, _receiver));
_obj.flush();
}
public static void serverEcho()
{
try{ ObjectInputStream objectin=new ObjectInputStream(_Socket.getInputStream());
Message newmess=(Message)objectin.readObject();
System.out.println(newmess.getAll());
}catch(IOException e){e.printStackTrace();}
catch(ClassNotFoundException e){e.printStackTrace();}
}
}
ServerPeer Class:
import java.net.*;
import java.io.*;
public class ServerPeer {
Socket _socket;
public ServerPeer(Socket _socket) {
this._socket = _socket;
}
public void methodCall() throws IOException, UnknownHostException,
ClassNotFoundException {
ObjectInputStream _ois = new ObjectInputStream(_socket.getInputStream());
Message _message;
while (_socket.isConnected()) {
_message = (Message) _ois.readObject();
System.out.println( _message.getAll());
_ois = new ObjectInputStream(_socket.getInputStream());
}
}
}
public void sendMessage(String _message) throws IOException {
ObjectOutputStream _obj = new ObjectOutputStream(_socket.getOutputStream());
_obj.writeObject(new Message(_username, _message));
_obj.flush();
}
我会将此方法添加到服务器,并在服务器收到消息时创建一个 if 语句...if(messageIsReceived)sendMessage(messageReceived);
我正在发送消息:
newbie:hi
文本出现在服务器控制台上=>服务器收到它但是当我回显时,为了将相同的消息打印到我的文本客户端控制台它没有显示这是我的回显代码及以下您还可以从 TextClient 中查看代码:(我做错了什么?它通常应该显示我从 TextClient 发送的消息)方法 getAll() returns 一个包含消息和发件人的字符串。
public void serverEcho() {
try {
ObjectInputStream objectin = new ObjectInputStream(_Socket
.getInputStream());
Message newmess = (Message) objectin.readObject();
System.out.println(newmess.getAll());
} catch (IOException e) {e.printStackTrace();
} catch (ClassNotFoundException e) {e.printStackTrace();
}
}
这是 TextClient:
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class TextClient {
public TextClient() {
}
public static void main(String[] args) throws IOException,
UnknownHostException, ConnectException, InterruptedException {
Socket _socket = new Socket("localhost", 9000);
System.out.println("Please enter your desired username:");
Scanner _new = new Scanner(System.in);
String _inputname = _new.nextLine();
System.out.println("Success.You can now type your messages!");
ClientPeer _clientpeer = new ClientPeer(_inputname, _socket);
Scanner _scan = new Scanner(System.in);
String _input = _scan.nextLine();
while (true) {
if (!_input.equals("exit")) {
if (_input.startsWith("/w"))
_clientpeer.sendMessage(_input, "username");
else {
_clientpeer.sendMessage(_input);
_clientpeer.serverEcho();
}
} else
break;
_input = _scan.nextLine();
}
}
}
ClientPeer class:
import java.net.*;
import java.io.*;
public class ClientPeer{
String _username;
static Socket _Socket;
public ClientPeer(String _username, Socket _Socket)
{
this._username = _username;
this._Socket = _Socket;
}
public void sendMessage(String _message) throws IOException {
ObjectOutputStream _obj = new ObjectOutputStream(
_Socket.getOutputStream());
_obj.writeObject(new Message(_username, _message));
_obj.flush();
}
public void sendMessage(String _message, String _receiver)
throws IOException {
ObjectOutputStream _obj = new ObjectOutputStream(
_Socket.getOutputStream());
_obj.writeObject(new PrivateMessage(_username, _message, _receiver));
_obj.flush();
}
public static void serverEcho()
{
try{ ObjectInputStream objectin=new ObjectInputStream(_Socket.getInputStream());
Message newmess=(Message)objectin.readObject();
System.out.println(newmess.getAll());
}catch(IOException e){e.printStackTrace();}
catch(ClassNotFoundException e){e.printStackTrace();}
}
}
ServerPeer Class:
import java.net.*;
import java.io.*;
public class ServerPeer {
Socket _socket;
public ServerPeer(Socket _socket) {
this._socket = _socket;
}
public void methodCall() throws IOException, UnknownHostException,
ClassNotFoundException {
ObjectInputStream _ois = new ObjectInputStream(_socket.getInputStream());
Message _message;
while (_socket.isConnected()) {
_message = (Message) _ois.readObject();
System.out.println( _message.getAll());
_ois = new ObjectInputStream(_socket.getInputStream());
}
}
}
public void sendMessage(String _message) throws IOException {
ObjectOutputStream _obj = new ObjectOutputStream(_socket.getOutputStream());
_obj.writeObject(new Message(_username, _message));
_obj.flush();
}
我会将此方法添加到服务器,并在服务器收到消息时创建一个 if 语句...if(messageIsReceived)sendMessage(messageReceived);