网络编程--使用TCP协议发送接收数据

package com.zhangxueliang.tcp;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/***
* 使用tcp协议发送接收数据
* @author zxlt
*
*/
public class ClientDemo { public static void main(String[] args) throws IOException {
//创建发送端Socket对象--创建连接
Socket s = new Socket(InetAddress.getByName("zxlt"),10000);
//获取输出流对象
OutputStream os = s.getOutputStream();
//发送数据
String str = "Hello TCP,I`m coming";
os.write(str.getBytes());
//释放资源
os.close();
s.close();
} }
package com.zhangxueliang.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket; public class ServerDemo { public static void main(String[] args) throws IOException {
//创建接收端Socket对象
ServerSocket ss = new ServerSocket(10000);
//监听 阻塞
Socket s = ss.accept();
//获取输入流对象
InputStream is = s.getInputStream();
//获取数据
byte[] bys = new byte[1024];
int length;//用于存储读到的字节个数
length = is.read(bys);
//输出数据
InetAddress address = s.getInetAddress();
System.out.println("client--> "+address.getHostAddress());
System.out.println(new String(bys,0,length));
//释放资源
s.close();
} }
上一篇:courator - maven


下一篇:【BootStrap】Table的基本使用