BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
第1层:字符缓冲输入流 第2层:字符转换输出流 第3层,字节输出流 getOutputStream,getInputStream都属于字节流
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys,0,len);
字符串转发成字节数组 str.getbyte[] 字节数组转换成字符串 String s= new String(byte[])
udp发送数据
import java.io.IOException;
import java.net.*;
public class senddemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
byte[] bys = "Hello,zzh".getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length,InetAddress.getByName("192.168.1.12"),10086);
ds.send(dp);
ds.close();
}
}
udp接受数据
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class recivedemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10086);
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
ds.receive(dp);
byte[] datas = dp.getData();
String dataString = new String(datas,0,dp.getLength());
System.out.println("数据是"+dataString);
ds.close();
}
}
用自己输入的数据实现udp
public class senddemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line=bf.readLine())!=null) {
if ("886".equals(line)){
break;
}
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.1.12"), 10086);
ds.send(dp);
}
ds.close();
}
}
public class recivedemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10086);
while (true) {
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
ds.receive(dp);
byte[] datas = dp.getData();
String dataString = new String(datas, 0, dp.getLength());
System.out.println("数据是" + dataString);
}
}
}
tcp网络编程联系
import java.io.*;
import java.net.*;
public class senddemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.1.8", 10000);
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys,0,len);
System.out.println("客户端"+data);
s.close();
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class servedemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10000);
Socket s = ss.accept();
InputStream is = s.getInputStream();
byte[] bys= new byte[1024];
int len = is.read(bys);
String data = new String(bys,0,len);
System.out.println("服务器"+data);
OutputStream os = s.getOutputStream();
os.write("数据已经收到".getBytes());
ss.close();
}
}
}
}
练习2
public class senddemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.1.8",10000);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line = br.readLine())!=null){
if("886".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
}
}
public class servedemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10000);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while((line = br.readLine())!=null){
System.out.println(line);
}
ss.close();
终结练习,给线程
import java.io.*;
import java.net.*;
public class senddemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.1.8",10000);
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line = br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
s.shutdownInput();
BufferedReader bbr =new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = bbr.readLine();
System.out.println("服务器的反馈是"+data);
br.close();
s.close();
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class servedemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10000);
while (true) {
Socket s = ss.accept();
new Thread(new ServerThread(s)).start();
}
}
}
import java.io.*;
import java.net.Socket;
public class ServerThread implements Runnable {
private Socket s;
public ServerThread(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
int count = 0;
File file = new File("myNet\\copy["+count+"].txt");
while (file.exists()){
count++;
file = new File("myNet\\copy["+count+"].txt");
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String line;
while ((line = br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
BufferedWriter bbw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bbw.write("文件上传成功");
bbw.newLine();
bbw.flush();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|