一个单线程java网络应用程序的简单实现
1.简单实现
1.1服务器代码
//服务器
import java.io.*;
import java.net.*;
import java.util.Scanner;
class Demo{
public static void main(String[] args) throws IOException {
Server ser=new Server();
ser.startService();
}
}
class Server{
private ServerSocket ServeSocket;
private InetAddress HostAddress;
private static final int port=8080;
private static final int queue=20;
private InputStream in;
private OutputStream out;
private DataInputStream DataInput;
private DataOutputStream DataOutput;
private Socket SocketReceived;
private void Init() throws IOException {
if(HostAddress==null)
HostAddress=InetAddress.getLocalHost();
if(ServeSocket==null)
ServeSocket=new ServerSocket(port,queue);
if(!ServeSocket.isBound())
ServeSocket.bind(new InetSocketAddress(port+20),queue);
System.out.println("启动成功!Host地址为:"+HostAddress.getHostAddress());
}
private void IOService() throws IOException {
Scanner s=new Scanner(System.in);
if(ServeSocket.isBound()&&!ServeSocket.isClosed())
{
System.out.println("等待连接...");
SocketReceived=ServeSocket.accept();
in=SocketReceived.getInputStream();
DataInput=new DataInputStream(in);
out=SocketReceived.getOutputStream();
DataOutput=new DataOutputStream(out);
System.out.println("已连接");
while(true) {
String MessageReceived = DataInput.readUTF();
System.out.println("来自用户的消息:" + MessageReceived);
System.out.print("回复:");
String reply = s.next();
DataOutput.writeUTF(reply);
}
}
}
void startService() throws IOException {
Init();
IOService();
}
}
1.2客户端代码
//客户端
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class User{
public static void main(String[] args) throws IOException {
ClientService c=new ClientService();
c.start();
}
}
class ClientService{
private Socket ClientSocket;
private InetSocketAddress HostAddress;
private InetSocketAddress ClientAddress;
private static final int HostPort=8080;
private static final int ClientPort=9090;
private InputStream in;
private OutputStream out;
private DataInputStream DataInput;
private DataOutputStream DataOutput;
private void Init() throws IOException {
HostAddress=new InetSocketAddress("10.138.107.110",HostPort);
if(ClientAddress==null)
ClientAddress=new InetSocketAddress("10.138.107.110",ClientPort);
if(ClientSocket==null)
ClientSocket=new Socket();
if(!ClientSocket.isBound())
ClientSocket.bind(ClientAddress);
}
private void IOService() throws IOException {
Scanner s=new Scanner(System.in);
if(ClientSocket.isBound()&&ClientSocket.isConnected())
{
in=ClientSocket.getInputStream();
DataInput=new DataInputStream(in);
out=ClientSocket.getOutputStream();
DataOutput=new DataOutputStream(out);
while(true) {
String message = s.next();
DataOutput.writeUTF(message);
String MessageReceived = DataInput.readUTF();
System.out.println("来自服务器的消息:" + MessageReceived);
System.out.print("回复:");
}
}
}
public void start() throws IOException {
Init();
ClientSocket.connect(HostAddress);
IOService();
}
}
2.TCP版本的实现
2.1服务器
//服务器/客户端撰写步骤总结:
1.配置地址、套接字
2.IO服务
//TCP version
//SERVER
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws IOException {
ServerService ser=new ServerService();
ser.start();
}
}
class ServerService{
private InetAddress Host;
private InetAddress User;
private DatagramPacket Data_Send;
private DatagramPacket Data_received;
private DatagramSocket ServerSocket;
private byte[] buf_Send;
private byte[] buf_Received;
private String temp_Receive;
private String temp_Send;
private static final int PORT_HOST=8080;
private static final int PORT_USER=9090;
private void Init() throws UnknownHostException, SocketException {
if(Host==null)
Host= Inet4Address.getLocalHost();
if(User==null)
User=Inet4Address.getLocalHost();
if(ServerSocket==null)
ServerSocket=new DatagramSocket(PORT_HOST);
}
private void InitIO(){
buf_Send=new byte[22];
Data_Send=new DatagramPacket(buf_Send,buf_Send.length);
Data_Send.setAddress(User);
Data_Send.setPort(PORT_USER);
}
void start() throws IOException {
Init();
InitIO();
System.out.println("等待消息。。。");
Scanner s=new Scanner(System.in);
while(1==1){
//接收消息
buf_Received=new byte[1024];
Data_received=new DatagramPacket(buf_Received,buf_Received.length);
ServerSocket.receive(Data_received);
temp_Receive=new String(Data_received.getData(),0,Data_received.getLength());
System.out.println("来自服务器的消息:"+temp_Receive);
//发送消息
System.out.print("请输入消息:");
temp_Send=s.next();
buf_Send=temp_Send.getBytes(StandardCharsets.UTF_8);
Data_Send.setData(buf_Send);
ServerSocket.send(Data_Send);
}
}
}
2.2客户端
//Clinet
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
class Demo{
public static void main(String[] args) throws IOException {
ClientService c=new ClientService();
c.start();
}
}
class ClientService{
private InetAddress Host;
private InetAddress User;
private DatagramPacket Data_Send;
private DatagramPacket Data_received;
private DatagramSocket ClientSocket;
private byte[] buf_Send;
private byte[] buf_Received;
private String temp_Receive;
private String temp_Send;
private static final int PORT_HOST=8080;
private static final int PORT_USER=9090;
private void Init() throws UnknownHostException, SocketException {
if(Host==null)
Host= Inet4Address.getLocalHost();
if(User==null)
User=Inet4Address.getLocalHost();
if(ClientSocket==null)
ClientSocket=new DatagramSocket(PORT_USER);
}
private void InitIO(){
buf_Send=new byte[22];
Data_Send=new DatagramPacket(buf_Send,buf_Send.length);
Data_Send.setAddress(Host);
Data_Send.setPort(PORT_HOST);
}
private Thread IN=new Thread();
private Thread OUT=new Thread();
Runnable work=new Runnable() {
@Override
public void run() {
}
}
void start() throws IOException {
Init();
InitIO();
ClientSocket.connect(Host,PORT_HOST);
Scanner s=new Scanner(System.in);
while(1==1){
//发送消息
System.out.print("请输入消息:");
temp_Send=s.next();
buf_Send=temp_Send.getBytes(StandardCharsets.UTF_8);
Data_Send.setData(buf_Send);
ClientSocket.send(Data_Send);
//接收消息
buf_Received=new byte[1024];
Data_received=new DatagramPacket(buf_Received,buf_Received.length);
ClientSocket.receive(Data_received);
temp_Receive=new String(Data_received.getData(),0,Data_received.getLength());
System.out.println("来自服务器的消息:"+temp_Receive);
}
}
}
3.小结
buf_Received,buf_Received.length);
ClientSocket.receive(Data_received);
temp_Receive=new String(Data_received.getData(),0,Data_received.getLength());
System.out.println(“来自服务器的消息:”+temp_Receive);
}
}
}
## 3.小结
上述内容是笔者很久以前学习javaSE时编写的简单功能,仅仅实现了一问一答式的单线程对话,且不具备前端界面,在功能性上距离真正的聊天室还有很大差距,故其中细节还有待进一步完善。