📄 jbdorderserver.java
字号:
import java.io.*;
import java.net.*;
public class JBDOrderServer
{
protected final static String CLIENT_ID = "JBDOrderClient";
protected final static double CLIENT_VERSION = 1.0;
protected final static String SERVER_ID = "JBDOrderServer";
protected final static double SERVER_VERSION = 1.0;
protected final static String HOST = "localhost";
protected final static int PORT = 5678;
private boolean terminated = false;
private PrintWriter out;
public JBDOrderServer()
{
try
{
out = new PrintWriter(new FileWriter("JBDOrder,dat", true));
}
catch (IOException ioe)
{
System.err.println("Error opening data ouput file(s)");
}
start();
}
public synchronized void writeOrder(CatalogOrder order)
{
out.println(order.exportData());
out.flush();
}
public void start()
{
try
{
ServerSocket server = new ServerSocket(PORT);
System.out.println(SERVER_ID + " " + SERVER_VERSION + " ready . . . ");
while (!terminated)
{
Socket connection = server.accept();
JBDOrderServerThread handler = new JBDOrderServerThread(this, connection);
}
System.out.println(SERVER_ID + " shutting down.");
server.close();
}
catch (IOException ioe)
{
System.err.println("Error establishing server and streams");
}
finally
{
out.close();
}
}
public static void main(String args[])
{
/*
try
{
ServerSocket server = new ServerSocket(PORT);
System.out.println("Server started . . . waiting . .");
Socket connection = server.accept();
System.out.println("Connection established. Identifying . . .");
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
if (in.readUTF().equals(CLIENT_ID))
{
out.writeUTF(SERVER_ID);
out.flush();
if (in.readDouble() == CLIENT_VERSION)
{
out.writeDouble(SERVER_VERSION);
out.flush();
try
{
CatalogOrder theBill = (CatalogOrder)in.readObject();
System.out.println(theBill);
}
catch (ClassNotFoundException cnfe)
{
System.err.println("Invalid class - contact support");
}
}
else
System.err.println("Invalid version - contact support");
}
else
System.out.println("Invalid client - contact support");
out.close();
in.close();
connection.close();
System.out.println("Connection closed.");
server.close();
}
catch (IOException ioe)
{
System.err.println("Error establishing server and streams");
}*/
JBDOrderServer server = new JBDOrderServer();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -