服务器客户端聊天程序
Server-Client Chatting program
我正在通过 console.When 我 运行 程序创建服务器客户端聊天程序,它只是无限地接受输入和输入。
Server code
import java.net.*;
import java.io.*;
public class Server {
static final int PORT_NUM=4458;
public static void main(String args []) {
try {
ServerSocket ss=new ServerSocket(PORT_NUM);
Socket s=ss.accept();
System.out.println("Server is listening...");
String str1="";
String str2="";
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
while(!str1.equals("STOP")) {
str1=din.readUTF();
System.out.println("Client says: "+str1);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
System.out.println("Connection closed");
din.close();
dout.close();
s.close();
ss.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Client code
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args []) {
try {
Socket s=new Socket("localhost",4458);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str1="";
String str2="";
while(!str1.equals("STOP")) {
str1=br.readLine();
dout.writeUTF(str1);
dout.flush();
str2=din.readUTF();
System.out.println("Client says: "+str2);
}
System.out.println("Connection closed");
din.close();
dout.close();
s.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
还有一个疑惑就是我运行这个eclipse上的程序IDE而且只有one console
。由于有两个不同的代码,所以必须有 two consoles
这样我将通过一个控制台代表服务器端提供输入,通过另一个控制台代表客户端提供输入。我应该怎么做才能做到这一点?
我无法理解我在哪里得到无限循环。我该怎么做请建议我。
至于无限循环,我似乎无法找出它发生的原因。我对 Eclipse 不是很熟悉,但 NetBeans 有一个功能,可以通过右键单击它并选择 运行 文件来启用 运行 具有 main 方法的 .java 文件。也许 Eclipse 有类似的功能?同样,您可以拥有两个 Eclipse 项目并 运行 它们分开,然后切换到您要访问的控制台。更好的是,您可以在 IDE 之外进行编码,然后 运行 使用两个命令提示符进行编码:一个用于服务器,一个用于客户端。这样您就可以“实时”查看消息。
以下代码对我有用。我在 IDE 之外输入它并使用命令提示符执行我的代码(服务器优先)。
服务器代码:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int port = 12345;
String stop = "STOP";
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Waiting for client connection..");
Socket socket = serverSocket.accept();
System.out.println("Connection received from " + socket.getInetAddress().getHostName());
String message = "";
Scanner scanner = new Scanner(System.in);
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
boolean closed = false;
while (!closed){
System.out.print("Enter message: ");
message = scanner.nextLine();
if (message.equalsIgnoreCase(stop)){
closed = true;
output.writeObject(stop);
System.out.println("Session ended.");
}
else{
output.writeObject(message);
System.out.println("Client typing...");
try {
message = (String) input.readObject();
System.out.println("Client says: " + message);
if (message.equalsIgnoreCase(stop)){
closed = true;
}
} catch (ClassNotFoundException ex) {
//Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error: " + ex.getMessage());
}
}
}
客户代码:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Client {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter server address: ");
String address = scanner.nextLine();
System.out.print("Enter server port: ");
int port = scanner.nextInt();
scanner.nextLine();//get rid of new line left by nextInt();
try {
Socket socket = new Socket(address, port);
System.out.println("Connected to server. Waiting for message");
String message = "";
String stop = "STOP";
boolean closed = false;
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
while (!closed){
try {
System.out.println("Server typing...");
message = (String) input.readObject();
System.out.println("Server says: " + message);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
if (message.equalsIgnoreCase(stop)){
closed = true;
}
else{
System.out.print("Enter message: ");
message = scanner.nextLine();
if (message.equalsIgnoreCase(stop)){
closed = true;
output.writeObject(stop);
System.out.println("Session ended.");
}
else{
output.writeObject(message);
}
}
}
System.out.println("Session ended.");
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error: " + ex.getMessage());
}
}
}
希望对您有所帮助。
您有两个主要方法,您可以 运行 在 Eclipse 中将它们作为两个不同的程序。假设您都在 Eclipse 中打开,您可以右键单击服务器代码和 select 运行。然后将 window 更改为客户端并重复。控制台将在底部的控制台 window 中标记。
对于循环,请记住以大写字母键入“STOP”。您还可以使用“equalsIgnoreCase()”,这样“STOP”和“stop”都可以工作。
我正在通过 console.When 我 运行 程序创建服务器客户端聊天程序,它只是无限地接受输入和输入。
Server code
import java.net.*;
import java.io.*;
public class Server {
static final int PORT_NUM=4458;
public static void main(String args []) {
try {
ServerSocket ss=new ServerSocket(PORT_NUM);
Socket s=ss.accept();
System.out.println("Server is listening...");
String str1="";
String str2="";
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
while(!str1.equals("STOP")) {
str1=din.readUTF();
System.out.println("Client says: "+str1);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
System.out.println("Connection closed");
din.close();
dout.close();
s.close();
ss.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Client code
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args []) {
try {
Socket s=new Socket("localhost",4458);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str1="";
String str2="";
while(!str1.equals("STOP")) {
str1=br.readLine();
dout.writeUTF(str1);
dout.flush();
str2=din.readUTF();
System.out.println("Client says: "+str2);
}
System.out.println("Connection closed");
din.close();
dout.close();
s.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
还有一个疑惑就是我运行这个eclipse上的程序IDE而且只有one console
。由于有两个不同的代码,所以必须有 two consoles
这样我将通过一个控制台代表服务器端提供输入,通过另一个控制台代表客户端提供输入。我应该怎么做才能做到这一点?
我无法理解我在哪里得到无限循环。我该怎么做请建议我。
至于无限循环,我似乎无法找出它发生的原因。我对 Eclipse 不是很熟悉,但 NetBeans 有一个功能,可以通过右键单击它并选择 运行 文件来启用 运行 具有 main 方法的 .java 文件。也许 Eclipse 有类似的功能?同样,您可以拥有两个 Eclipse 项目并 运行 它们分开,然后切换到您要访问的控制台。更好的是,您可以在 IDE 之外进行编码,然后 运行 使用两个命令提示符进行编码:一个用于服务器,一个用于客户端。这样您就可以“实时”查看消息。 以下代码对我有用。我在 IDE 之外输入它并使用命令提示符执行我的代码(服务器优先)。 服务器代码:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int port = 12345;
String stop = "STOP";
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Waiting for client connection..");
Socket socket = serverSocket.accept();
System.out.println("Connection received from " + socket.getInetAddress().getHostName());
String message = "";
Scanner scanner = new Scanner(System.in);
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
boolean closed = false;
while (!closed){
System.out.print("Enter message: ");
message = scanner.nextLine();
if (message.equalsIgnoreCase(stop)){
closed = true;
output.writeObject(stop);
System.out.println("Session ended.");
}
else{
output.writeObject(message);
System.out.println("Client typing...");
try {
message = (String) input.readObject();
System.out.println("Client says: " + message);
if (message.equalsIgnoreCase(stop)){
closed = true;
}
} catch (ClassNotFoundException ex) {
//Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error: " + ex.getMessage());
}
}
}
客户代码:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Client {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter server address: ");
String address = scanner.nextLine();
System.out.print("Enter server port: ");
int port = scanner.nextInt();
scanner.nextLine();//get rid of new line left by nextInt();
try {
Socket socket = new Socket(address, port);
System.out.println("Connected to server. Waiting for message");
String message = "";
String stop = "STOP";
boolean closed = false;
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
while (!closed){
try {
System.out.println("Server typing...");
message = (String) input.readObject();
System.out.println("Server says: " + message);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
if (message.equalsIgnoreCase(stop)){
closed = true;
}
else{
System.out.print("Enter message: ");
message = scanner.nextLine();
if (message.equalsIgnoreCase(stop)){
closed = true;
output.writeObject(stop);
System.out.println("Session ended.");
}
else{
output.writeObject(message);
}
}
}
System.out.println("Session ended.");
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error: " + ex.getMessage());
}
}
}
希望对您有所帮助。
您有两个主要方法,您可以 运行 在 Eclipse 中将它们作为两个不同的程序。假设您都在 Eclipse 中打开,您可以右键单击服务器代码和 select 运行。然后将 window 更改为客户端并重复。控制台将在底部的控制台 window 中标记。
对于循环,请记住以大写字母键入“STOP”。您还可以使用“equalsIgnoreCase()”,这样“STOP”和“stop”都可以工作。