Java 双向套接字连接
Java Two-Way Socket Connection
我正在 Java 中致力于在客户端和服务器之间生成基本的双向套接字连接。按照我在网上其他地方找到的一些指南,我让它以一种方式工作,服务器从客户端接收消息,但我很好奇我将如何制作它以便客户端和服务器可以相互发送消息。我也在努力实现有效载荷,但在我设法使双向连接正常工作之前,我并不担心这个。
这是我的服务器代码:
import java.net.*;
import java.io.*;
public class ServerDemo {
private Socket mySocket = null;
private ServerSocket server = null;
private DataInputStream input = null;
public ServerDemo(int port)
{
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
mySocket = server.accept();
System.out.println("Client accepted");
input = new DataInputStream(
new BufferedInputStream(mySocket.getInputStream()));
String newLine = "";
while (!newLine.equals("Over"))
{
try
{
newLine = input.readUTF();
System.out.println(newLine);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
mySocket.close();
input.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerDemo server = new ServerDemo(5000);
}
}
这是客户端的代码:
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class ClientDemo {
private Socket mySocket = null;
private BufferedReader input= null;
private DataOutputStream output = null;
public ClientDemo(String address, int port)
{
try
{
mySocket = new Socket(address, port);
System.out.println("Connected");
input = new BufferedReader(new InputStreamReader(System.in));
output = new DataOutputStream(mySocket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
String newLine = "";
while (!newLine.equals("Over"))
{
try
{
newLine = input.readLine();
output.writeUTF(newLine);
}
catch(IOException i)
{
System.out.println(i);
}
}
try
{
input.close();
output.close();
mySocket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String[] args) {
String response="";
Scanner myScanner = new Scanner(System.in);
System.out.println("Please enter a server to connect to.");
response=myScanner.next();
ClientDemo client = new ClientDemo(response, 5000);
}
}
对于如何将其转换为双向通信的任何建议,我将不胜感激。
为了让服务器向客户端发送回消息,您必须使用 mySocket.getOutputStream()
[= 从服务器 (ServerDemo) 响应客户端16=]
有多种方法,
BufferedWriter buffer = new BufferedWriter(new OutputStreamWriter(mySocket.getOutputStream(), "UTF8"));
buffer.append(stringToSend).append("\n");
buffer.append("Over").append("\n");
buffer.flush();
PrintWriter out = new PrintWriter(mySocket.getOutputStream());
String response = "Data received successfully";
out.print(response);
要打印来自服务器的响应,可以在 ClientDemo try/catch 块[=32] 中使用以下代码片段=] 套接字关闭的地方。
BufferedReader bufferIn=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
String message;
while(!(message = bufferIn.readLine()).equals("Over")){
System.out.println(message);
}
我正在 Java 中致力于在客户端和服务器之间生成基本的双向套接字连接。按照我在网上其他地方找到的一些指南,我让它以一种方式工作,服务器从客户端接收消息,但我很好奇我将如何制作它以便客户端和服务器可以相互发送消息。我也在努力实现有效载荷,但在我设法使双向连接正常工作之前,我并不担心这个。
这是我的服务器代码:
import java.net.*;
import java.io.*;
public class ServerDemo {
private Socket mySocket = null;
private ServerSocket server = null;
private DataInputStream input = null;
public ServerDemo(int port)
{
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
mySocket = server.accept();
System.out.println("Client accepted");
input = new DataInputStream(
new BufferedInputStream(mySocket.getInputStream()));
String newLine = "";
while (!newLine.equals("Over"))
{
try
{
newLine = input.readUTF();
System.out.println(newLine);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
mySocket.close();
input.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerDemo server = new ServerDemo(5000);
}
}
这是客户端的代码:
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class ClientDemo {
private Socket mySocket = null;
private BufferedReader input= null;
private DataOutputStream output = null;
public ClientDemo(String address, int port)
{
try
{
mySocket = new Socket(address, port);
System.out.println("Connected");
input = new BufferedReader(new InputStreamReader(System.in));
output = new DataOutputStream(mySocket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
String newLine = "";
while (!newLine.equals("Over"))
{
try
{
newLine = input.readLine();
output.writeUTF(newLine);
}
catch(IOException i)
{
System.out.println(i);
}
}
try
{
input.close();
output.close();
mySocket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String[] args) {
String response="";
Scanner myScanner = new Scanner(System.in);
System.out.println("Please enter a server to connect to.");
response=myScanner.next();
ClientDemo client = new ClientDemo(response, 5000);
}
}
对于如何将其转换为双向通信的任何建议,我将不胜感激。
为了让服务器向客户端发送回消息,您必须使用 mySocket.getOutputStream()
[= 从服务器 (ServerDemo) 响应客户端16=]
有多种方法,
BufferedWriter buffer = new BufferedWriter(new OutputStreamWriter(mySocket.getOutputStream(), "UTF8"));
buffer.append(stringToSend).append("\n");
buffer.append("Over").append("\n");
buffer.flush();
PrintWriter out = new PrintWriter(mySocket.getOutputStream());
String response = "Data received successfully";
out.print(response);
要打印来自服务器的响应,可以在 ClientDemo try/catch 块[=32] 中使用以下代码片段=] 套接字关闭的地方。
BufferedReader bufferIn=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
String message;
while(!(message = bufferIn.readLine()).equals("Over")){
System.out.println(message);
}