码迷,mamicode.com
首页 > 编程语言 > 详细

java socket服务器端搭建

时间:2014-07-14 18:34:42      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:java   socket   服务器   thread   class   

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服务器端搭建,布布扣,bubuko.com

java socket服务器端搭建

标签:java   socket   服务器   thread   class   

原文地址:http://blog.csdn.net/maskdfe/article/details/37764565

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!