1public static void main(String[] args) throws IOException {
2 //創建數據包對象,封裝要發送的數據,接收端Ip,端口號
3 byte[] data="123udp".getBytes();
4 //創建一個InetAddress對象,封裝自己的Ip地址
5 InetAddress inetAddress =InetAddress.getByName("127.0.0.1");
6 DatagramPacket dp =new DatagramPacket(data, data.length,inetAddress,8899);
7 //創建DatagramSocket對象,
8 DatagramSocket datagramSocket=new DatagramSocket();
9 datagramSocket.send(dp);
10 datagramSocket.close();
11}
1public static void main(String[] args) throws IOException {
2 //創建數據包傳輸對象,并綁定端口號
3 DatagramSocket ds =new DatagramSocket(8899);
4 //創建字節數組
5 byte[] buf=new byte[1024];
6 //創建數據包對象傳遞字節數組
7 DatagramPacket dp =new DatagramPacket(buf, buf.length);
8 //調用ds的receive傳遞數組
9 ds.receive(dp);
10 String ip =dp.getAddress().getHostAddress();
11 int port =dp.getPort();
12 int length=dp.getLength();
13 System.out.println(new String(buf,0,length)+"..."+ip+"..."+port);
14 ds.close();
15}
1public static void main(String[] args) throws IOException {
2 Scanner sc=new Scanner(System.in);
3 DatagramSocket datagramSocket=new DatagramSocket();
4 InetAddress inetAddress =InetAddress.getByName("127.0.0.1");
5 while(true){
6 String message=sc.nextLine();
7 byte[] data=message.getBytes();
8 DatagramPacket dp =new DatagramPacket(data, data.length,inetAddress,8899);
9 datagramSocket.send(dp);
10 }
11}
1public static void main(String[] args) throws IOException {
2 //創建數據包傳輸對象,并綁定端口號
3 DatagramSocket ds =new DatagramSocket(8899);
4 //創建字節數組
5 byte[] buf=new byte[1024];
6 //創建數據包對象傳遞字節數組
7 while(true){
8 DatagramPacket dp =new DatagramPacket(buf, buf.length);
9 //調用ds的receive傳遞數組
10 ds.receive(dp);
11 String ip =dp.getAddress().getHostAddress();
12 int port =dp.getPort();
13 int length=dp.getLength();
14 System.out.println(new String(buf,0,length)+"..."+ip+"..."+port);
15 }
16}
1public static void main(String[] args) throws IOException {
2 Socket socket=new Socket("120.27.60.73", 8899);
3 OutputStream outStream=socket.getOutputStream();
4 outStream.write("789456".getBytes());
5 socket.close();
6}
1public static void main(String[] args) throws IOException {
2 ServerSocket serverSocket=new ServerSocket(8899);
3 Socket socket=serverSocket.accept();
4 InputStream inputStream=socket.getInputStream();
5 byte[] buf=new byte[1024];
6 int len=inputStream.read(buf);
7 System.out.println(new String(buf,0,len));
8 //服務器返回數據
9 OutputStream out=socket.getOutputStream();
10 out.write("nihao".getBytes());
11 socket.close();
12 serverSocket.close();
13}
1public class TCPClient {
2 public static void main(String[] args) throws IOException{
3 Socket socket = new Socket("127.0.0.1", 8000);
4 //獲取字節輸出流,圖片寫到服務器
5 OutputStream out = socket.getOutputStream();
6 //創建字節輸入流,讀取本機上的數據源圖片
7 FileInputStream fis = new FileInputStream("c:\\t.jpg");
8 //開始讀寫字節數組
9 int len = 0 ;
10 byte[] bytes = new byte[1024];
11 while((len = fis.read(bytes))!=-1){
12 out.write(bytes, 0, len);
13 }
14 //給服務器寫終止序列
15 socket.shutdownOutput();
16
17 //獲取字節輸入流,讀取服務器的上傳成功
18 InputStream in = socket.getInputStream();
19
20 len = in.read(bytes);
21 System.out.println(new String(bytes,0,len));
22
23 fis.close();
24 socket.close();
25 }
26}
1public class Upload implements Runnable{
2
3 private Socket socket;
4
5 public Upload(Socket socket){this.socket=socket;}
6
7 public void run() {
8 try{
9 //通過客戶端連接對象,獲取字節輸入流,讀取客戶端圖片
10 InputStream in = socket.getInputStream();
11 //將目的文件夾封裝到File對象
12 File upload = new File("d:\\upload");
13 if(!upload.exists())
14 upload.mkdirs();
15
16 //防止文件同名被覆蓋,從新定義文件名字
17 //規則: 域名+毫秒值+6位隨機數
18 String filename="itcast"+System.currentTimeMillis()+new Random().nextInt(999999)+".jpg";
19 //創建字節輸出流,將圖片寫入到目的文件夾中
20 FileOutputStream fos = new FileOutputStream(upload+File.separator+filename);
21 //讀寫字節數組
22 byte[] bytes = new byte[1024];
23 int len = 0 ;
24 while((len = in.read(bytes))!=-1){
25 fos.write(bytes, 0, len);
26 }
27 //通過客戶端連接對象獲取字節輸出流
28 //上傳成功寫回客戶端
29 socket.getOutputStream().write("上傳成功".getBytes());
30
31 fos.close();
32 socket.close();
33 }catch(Exception ex){
34
35 }
36 }
37
38}
1public class TCPThreadServer {
2 public static void main(String[] args) throws IOException{
3 ServerSocket server = new ServerSocket(8000);
4 while(true){
5 //獲取到一個客戶端,必須開啟新線程
6 Socket socket = server.accept();
7 new Thread( new Upload(socket) ).start();
8 }
9
10 }
11}
1String fileName="xdclass"+System.currentTimeMillis()+new Ranadow.nextInt(999999)+".jpg";
1File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt");
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。