码迷,mamicode.com
首页 > 其他好文 > 详细

史上最简单的socket

时间:2015-09-14 13:42:18      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

 

1. client

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;


public class TcpClient2 {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Socket s = new Socket("192.168.1.120",9998);
		OutputStream os = s.getOutputStream();
		os.write("i will go".getBytes());
		os.close();
		s.close();
	}

}
            

  

2. Server

 

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class TcpServer2 {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		ServerSocket ss = new ServerSocket(9998);
		System.out.println(ss.getInetAddress());
		Socket s = ss.accept();
		InputStream is= s.getInputStream();
		byte[] buf = new byte[1024];
		int len = is.read(buf);
		System.out.println(new String(buf,0,len));
	}

}

  

史上最简单的socket

标签:

原文地址:http://www.cnblogs.com/childhooding/p/4806874.html

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