java socket服务器端搭建

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;

public class ServerBase extends Thread {

	public static void main(String[] args) {
		ServerBase.startServer(8809, ServerBase.class);
	}

	protected Socket socket;

	public void run() {
		try {
			byte[] b = new byte[100]; 
			socket.getInputStream().read(b);
				byte[] out = { (byte) 0xa7, (byte) 0xcb };
				socket.getOutputStream().write(out);
		
			socket.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	@SuppressWarnings({ "resource", "rawtypes" })
	public static void startServer(int port, Class clobj) {
		ServerSocket ssock;
		try {
			ssock = new ServerSocket(port);
			System.out.println("Listening...");
			while (true) {
				Socket esock = null;
				try {
					esock = ssock.accept();
					System.out.println("connected...");
					ServerBase t = (ServerBase) clobj.newInstance();
					t.socket = esock;
					t.start();
				} catch (Exception e) {
					try {
						esock.close();
					} catch (Exception ec) {
					}
				}
			}
		} catch (IOException e) {
		}
	}

}

java socket服务器端搭建,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。