TCP发送接收结合IO流
public class test6 {
private static Scanner sc=new Scanner(System.in);
public static void main(String[] args) throws IOException {
Socket socket=new Socket("127.0.0.1",8888);
System.out.println("输入想要复制的文件目录:");
String s1 = sc.nextLine();
FileInputStream s = new FileInputStream(s1);
File files = new File(s1);
if (files.exists()&&files.getName().contains("jpg")&&files.length()<1024*1024*2){
System.out.println("文件存在小于2M且格式为jpg格式");
BufferedInputStream bis=new BufferedInputStream(s);
OutputStream os=socket.getOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(os);
int b;
while ((b=bis.read())!=-1){
bos.write(b);
}
bos.flush();
socket.shutdownOutput();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
bis.close();
socket.close();
}else {
try {
System.out.println("文件有误");
}catch (Exception e){
e.getMessage();
}
}
}
static class serverdemo{
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(8888);
Socket accept=ss.accept();
BufferedInputStream bis=new BufferedInputStream(accept.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a\\copy.jpg"));
int b;
while ((b=bis.read())!=-1){
bos.write(b);
}
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
bw.write("上传成功");
bw.newLine();
bw.flush();
bos.close();
accept.close();
ss.close();
}
}
}
网络编程单播
public class test03 {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket();
Scanner sc=new Scanner(System.in);
while (true){
String s=sc.nextLine();
if (s.equals("886")){
break;
}
byte[] bytes=s.getBytes();
DatagramPacket dp=new DatagramPacket(bytes, bytes.length,InetAddress.getByName("127.0.0.1"),10000);
ds.send(dp);
}
ds.close();
}
static class receivedemo{
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket(10000);
while (true){
byte[] bytes=new byte[1024];
DatagramPacket dp=new DatagramPacket(bytes, bytes.length);
ds.receive(dp);
System.out.println("数据是:"+new String(dp.getData(),0,dp.getLength()));
}
}
}
}
网络编程组播
public class test04 {
private static Scanner sc=new Scanner(System.in);
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket();
while (true){
String s=sc.nextLine();
if (s.equals("886")){
break;
}
byte[] bytes=s.getBytes();
DatagramPacket dp=new DatagramPacket(bytes, bytes.length,InetAddress.getByName("224.0.1.1"),10001);
ds.send(dp);
}
ds.close();
}
}
public class test05 {
public static void main(String[] args) throws IOException {
MulticastSocket ms = new MulticastSocket(10001);
ms.joinGroup(InetAddress.getByName("224.0.1.1"));
while (true) {
DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
ms.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data, 0, length));
}
}
}
网络编程结合线程实时接收发送
public class test8_1 {
private static boolean b=false;
private static Scanner sc=new Scanner(System.in);
public static void main(String[] args) throws InterruptedException, SocketException {
Object lock=new Object();
Thread t1 = new Thread(() -> {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
while (true) {
synchronized (lock) {
String s = sc.nextLine();
if ("2b".equals(s)) {
break;
}
byte[] bytes = s.getBytes();
DatagramPacket dp = null;
try {
dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.95.42"), 12345);
} catch (UnknownHostException e) {
e.printStackTrace();
}
try {
ds.send(dp);
} catch (IOException e) {
e.printStackTrace();
}
b=true;
lock.notify();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
DatagramSocket ds1 = new DatagramSocket(12345);
Thread t2 = new Thread(() -> {
while (true){
synchronized (lock) {
if (b == true){
byte[] bytes=new byte[1024];
DatagramPacket dp=new DatagramPacket(bytes, bytes.length);
try {
ds1.receive(dp);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("数据是:"+new String(dp.getData(),0,dp.getLength()));
b=false;
lock.notify();
try {
lock.wait();
}
catch (Exception e) {
e.printStackTrace();
}
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
t2.start();
t1.start();
}
}