您的位置:首页 > 其它

史上最简单的socket

2015-09-14 12:45 246 查看
原文链接:https://www.geek-share.com/detail/2653205900.html

 

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));
}

}

  

转载于:https://www.cnblogs.com/childhooding/p/4806874.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: