gpt4 book ai didi

java - 服务器,客户端套接字实现

转载 作者:行者123 更新时间:2023-11-30 08:50:08 24 4
gpt4 key购买 nike

我正在使用 Java Socket 编写服务器、客户端聊天程序。这是我的服务器套接字类代码。

import java.io.*;
import java.net.*;

public class Main {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8085);
} catch (IOException ex) {
System.out.println("IO Error, " + ex);
System.exit(1);
}
Socket clientSocket = null;
System.out.println("Listening for incoming connections");
try {
clientSocket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("Failed to accept connection " + ex);
System.exit(1);
}
System.out.println("Connection Successful");
System.out.println("Listening to get input");
PrintStream output = new PrintStream(clientSocket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = input.readLine()) != null) {
System.out.println(inputLine);
System.out.println("Server: ");
inputLine = input.readLine();
output.println(inputLine);
if (!inputLine.equals("exit")) {

} else {
break;
}
}
output.close();
input.close();
clientSocket.close();
serverSocket.close();
}
}

客户端能够建立连接并向服务器发送消息。服务器也可以接收客户端发送的消息。问题是当消息从服务器发送时,客户端没有收到消息。这是我的客户端套接字代码。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;


public class Client {
public static void main(String [] args) throws Exception
{
BufferedReader input;
PrintStream output;
BufferedReader clientInput;
try (Socket client = new Socket("127.0.0.1", 8085)) {
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
output = new PrintStream(client.getOutputStream());
clientInput = new BufferedReader(new InputStreamReader(System.in));
String line;
while(true)
{
System.out.println("Client: ");
line = clientInput.readLine();
output.println("Server: " + line );
if(line.equals("quit"))
{
break;
}
}
}
input.close();
clientInput.close();
output.close();
}
}

最佳答案

服务器端:

public class Server {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8085);
} catch (IOException ex) {
System.out.println("IO Error, " + ex);
System.exit(1);
}
Socket clientSocket = null;
System.out.println("Listening for incoming connections");
try {
clientSocket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("Failed to accept connection " + ex);
System.exit(1);
}
System.out.println("Connection Successful");
System.out.println("Listening to get input");
PrintStream output = new PrintStream(clientSocket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = input.readLine()) != null) {

System.out.println("Client request: " + inputLine);

String resp = "some response as you need";
output.println(resp);
System.out.println("Server response: " + resp);

if (!inputLine.equals("exit")) {

} else {
break;
}
}
output.close();
input.close();
clientSocket.close();
serverSocket.close();
}
}

客户端:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Client {

public static void main(String[] args) throws Exception {

BufferedReader input;
PrintStream output;
BufferedReader clientInput;
try (Socket client = new Socket("127.0.0.1", 8085)) {
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
output = new PrintStream(client.getOutputStream());
clientInput = new BufferedReader(new InputStreamReader(System.in));

while (true) {

String inputStr = clientInput.readLine();

output.println(inputStr);
System.out.println("Client: " + inputStr);

if (inputStr.equals("quit")) {
break;
}

String serverResp = input.readLine();
output.println("Server: " + serverResp);
}
}
}
}

已测试。

关于java - 服务器,客户端套接字实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31124188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com