UDP的使用
public class A {
public static void main(String[] args) throws IOException {
new Thread() {
@Override
public void run() {
System.out.println("服务器已经开启,等待接收数据....");
try {
DatagramSocket ds = new DatagramSocket(9999);
while (true) {
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
ds.receive(dp);
//取出数据报包中的数据
byte[] data = dp.getData();
int length = dp.getLength();
//从发来的数据报包中,也可也取出发送者的IP
InetAddress address = dp.getAddress();
String hostAddress = address.getHostAddress(); //取出发送者的ip
String s = new String(data, 0, length);
System.out.println("B:" + hostAddress + "-给你发来消息:" + s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
//主线程:发消息 发到A的服务端
DatagramSocket ds = new DatagramSocket();
sendMsg(ds);//发消息
//开启子线程 接收消息:服务端
}
private static void sendMsg(DatagramSocket ds) throws IOException {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入发送的消息");
String str = scanner.nextLine();
if (str.equals("886")) { //自定义结束标记
break;
}
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("192.168.16.123"), 8888);
ds.send(dp);
}
ds.close();
}
}
Tcp的使用
public class UploadThread extends Thread{
Socket sk;
public UploadThread(Socket sk) {
this.sk=sk;
}
@Override
public void run() {
try {
//获取通道中的输入输出流
InputStream in = sk.getInputStream();
//包装通道中的输入流
BufferedReader bfr = new BufferedReader(new InputStreamReader(in));
//文件名随机取,防止覆盖
BufferedWriter bfw = new BufferedWriter(new FileWriter(System.currentTimeMillis()+"TCPClient2.txt"));
String line = null;
while ((line = bfr.readLine()) != null) {
bfw.write(line);
bfw.newLine();
bfw.flush();
}
//当服务端,保存完文件,给给客户一个反馈
OutputStream out = sk.getOutputStream();
out.write("文件上传成功".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
class TCPServer {
public static void main(String[] args) throws IOException {
//实现一个服务端,要连接多个客户端
ServerSocket ss = new ServerSocket(8888);
System.out.println("服务器已经开启,等待连接...");
int i=0;
//写个死循环,连接多个客户端
while (true){
System.out.println("第"+(i++)+"个客户端已经连接");
Socket sk = ss.accept(); //监听客户端
//每个客户连接上来,就开启线程去处理
new UploadThread(sk).start();
}
// ss.close();
}
}
class TCPClinet {
public static void main(String[] args) throws IOException {
Socket sk = new Socket("192.168.16.123", 8888);
//获取通道中的输出流
OutputStream out = sk.getOutputStream();
BufferedReader bfr = new BufferedReader(new FileReader("TCPClient.java"));
//把通道中的字节流,包装字符流
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(out));
//客户端把从文本文件中读取到的数据发送到 服务端
String line = null;
while ((line = bfr.readLine()) != null) {
//自定义一个结束标记
bfw.write(line);
bfw.newLine();
bfw.flush();
}
//客户端肯定把文件发送完了,手动写一个标记给服务端
/* bfw.write("over");
bfw.newLine();
bfw.flush();*/
/* void shutdownInput ()
此套接字的输入流置于“流的末尾”。
void shutdownOutput ()
禁用此套接字的输出流。*/
sk.shutdownOutput();
//读取反馈
InputStream in = sk.getInputStream();
byte[] bytes = new byte[1024];
int len = in.read(bytes);//阻塞式的
String s = new String(bytes, 0, len);
System.out.println(s);
sk.close();
}
}