⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comserver.java

📁 一个简单而精彩的java聊天室小程序
💻 JAVA
字号:
package server;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class comServer {
	public ServerSocket svrSkt = null;
	public Socket cltSkt = null;
	public DataInputStream input = null;
	public PrintStream output = null;
	
	public comServer(int port) {
		System.out.println("listening, port: " + port);
		try {
			svrSkt = new ServerSocket(port);
		}catch(IOException e) {
			System.out.println("listen port " + port);
		}
		
		try {
			cltSkt = svrSkt.accept();
		}catch(IOException e) {
			System.out.println("connect faild");
		}
		
		try {
			input = new DataInputStream(cltSkt.getInputStream());
			output = new PrintStream(cltSkt.getOutputStream());
		}catch(IOException e) {
			
		}
		
		output.println("Welcome......");
	}
	
	@SuppressWarnings("deprecation")
	public String getRequest() {
		String frmClt = null;
		try {
			frmClt = input.readLine();
		}catch(Exception e) {
			System.out.println("can't readed from port...");
			System.exit(0);
		}
		
		return frmClt;
	}
	
	public void sendResponse(String response) {
		try {
			output.println(response);
		}catch(Exception e) {
			System.out.println("write to port faild...");
			System.exit(0);
		}
	}
	
	public static void main(String[] args) throws IOException {
		comServer sa = new comServer(1001);
		while (true) {
			sa.sendResponse(sa.getRequest());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -