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

TCP即时小通信

时间:2015-08-06 20:20:54      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

package 第十二章;
import java.io.*;
import java.net.*;
public class TcpServer {
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		ServerSocket server=new ServerSocket(55555);
		Socket s=server.accept();
		InputStream is =s.getInputStream();
		byte []b=new byte[1024];
		int length =is.read(b);
		
		String msg=new String (b,0,length);
		System.out.println(msg);
		
		is.close();
		s.close();
		server.close();
	}

}


//客户端
package 第十二章;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class TcpClient {
	public static void main(String[] args) throws UnknownHostException, IOException{
		// TODO Auto-generated method stub
		while(true){
			Scanner scan=new Scanner(System.in);
			System.out.println("请输入你要输入的内容:");
			String str=scan.next();
			
			Socket s=new Socket("192.168.1.179",55555);
			OutputStream os=s.getOutputStream();
			
			os.write(str.getBytes());
			os.close();
			s.close();
		}
	}

}

 

TCP即时小通信

标签:

原文地址:http://www.cnblogs.com/xiaoqisfzh/p/4708961.html

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