Vinh La Kiến

48 bundles
3 files4 months ago
1

Java Socket code demo

Kết nối với Java Socket, code demo cho Server và Client.

Socket Server

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
package com.company; import java.io.*; import java.net.UnknownHostException; import java.net.*; import java.nio.ByteBuffer; public class Main { public static void main(String[] args) throws FileNotFoundException, InterruptedException, UnknownHostException { try { Socket socket = new Socket("192.168.0.101", 9100); OutputStream outputStream = socket.getOutputStream(); OutputStreamWriter outputStreamReader = new OutputStreamWriter(outputStream); outputStreamReader.write("Số hóa đơn:01234M\n" + "13/04/2020\n" + "===============\n" + "Cá thu 1 30.000đ\n"); outputStreamReader.close(); outputStream.close(); socket.close(); return; InputStream inputStream = socket.getInputStream(); byte[] fileSize = new byte[4]; inputStream.read(fileSize, 0, 1); inputStream.read(fileSize, 1, 1); inputStream.read(fileSize, 2, 1); inputStream.read(fileSize, 3, 1); int fileSize2 = ByteBuffer.wrap(fileSize).getInt(); byte[] data = null; if (fileSize2 < 100 * 1024 * 1024) { data = new byte[fileSize2]; int readedSize = 0; byte[] buffer = new byte[1000]; int readingSize; while((readingSize = inputStream.read(buffer, 0, buffer.length)) > -1) { System.arraycopy(buffer, 0, data, readedSize, readingSize); readedSize += readingSize; } } else { int trackingFileSize = 0; while (trackingFileSize < fileSize2) { data = new byte[100 * 1024 * 1024]; data = new byte[fileSize2]; int readedSize = 0; byte[] buffer = new byte[1000]; int readingSize; while((readingSize = inputStream.read(buffer, 0, buffer.length)) > -1) { System.arraycopy(buffer, 0, data, readedSize, readingSize); readedSize += readingSize; } // dump data -> file trackingFileSize += readedSize; } } /* int size = inputStream.read(); byte[] data = new byte[0]; byte[] buffer = new byte[1000]; int readedSize; while((readedSize = inputStream.read(buffer, 0, buffer.length)) > -1) { byte[] temp = new byte[data.length + readedSize]; System.arraycopy(data, 0, temp,0, data.length); System.arraycopy(buffer, 0, temp, buffer.length, readedSize); data = temp; } String text = new String(data); System.out.println(text); */ /* InputStreamReader inputStreamReader = new InputStreamReader(inputStream); StringBuilder text = new StringBuilder(); int character; while((character = inputStreamReader.read()) != -1) { text.append((char)character); } System.out.println(text); */ } catch (IOException e) { e.printStackTrace(); } } } // 132.163.96.6 // 132.163.97.6 // 129.6.15.30 // 132.163.97.1

Socket Client

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
package com.company; import java.io.*; import java.net.UnknownHostException; import java.net.*; import java.nio.ByteBuffer; public class Main { // time.nist.gov => IP public static void main(String[] args) throws FileNotFoundException, InterruptedException, UnknownHostException { try { Socket socket = new Socket("time.nist.gov", 13); InputStream inputStream = socket.getInputStream(); byte[] fileSize = new byte[4]; inputStream.read(fileSize, 0, 1); inputStream.read(fileSize, 1, 1); inputStream.read(fileSize, 2, 1); inputStream.read(fileSize, 3, 1); int fileSize2 = ByteBuffer.wrap(fileSize).getInt(); byte[] data = null; if (fileSize2 < 100 * 1024 * 1024) { data = new byte[fileSize2]; int readedSize = 0; byte[] buffer = new byte[1000]; int readingSize; while((readingSize = inputStream.read(buffer, 0, buffer.length)) > -1) { System.arraycopy(buffer, 0, data, readedSize, readingSize); readedSize += readingSize; } } else { int trackingFileSize = 0; while (trackingFileSize < fileSize2) { data = new byte[100 * 1024 * 1024]; data = new byte[fileSize2]; int readedSize = 0; byte[] buffer = new byte[1000]; int readingSize; while((readingSize = inputStream.read(buffer, 0, buffer.length)) > -1) { System.arraycopy(buffer, 0, data, readedSize, readingSize); readedSize += readingSize; } // dump data -> file trackingFileSize += readedSize; } } /* int size = inputStream.read(); byte[] data = new byte[0]; byte[] buffer = new byte[1000]; int readedSize; while((readedSize = inputStream.read(buffer, 0, buffer.length)) > -1) { byte[] temp = new byte[data.length + readedSize]; System.arraycopy(data, 0, temp,0, data.length); System.arraycopy(buffer, 0, temp, buffer.length, readedSize); data = temp; } String text = new String(data); System.out.println(text); */ /* InputStreamReader inputStreamReader = new InputStreamReader(inputStream); StringBuilder text = new StringBuilder(); int character; while((character = inputStreamReader.read()) != -1) { text.append((char)character); } System.out.println(text); */ } catch (IOException e) { e.printStackTrace(); } } } // 132.163.97.6 // 129.6.15.30

Socket Server - Sync

12345678910111213141516171819202122232425262728293031323334353637383940
package com.company; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; import java.net.ServerSocket; public class Main { public static void main(String[] args) { Integer port = 12345; try { ServerSocket serverSocket = new ServerSocket(port); System.out.println("Server start at port " + port.toString()); Socket socket = serverSocket.accept(); System.out.println("A client connected to server"); InputStream inputStream = socket.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); StringBuilder message = new StringBuilder(); int character; while((character = inputStreamReader.read()) != -1) { message.append((char)character); } System.out.println(message); inputStreamReader.close(); inputStream.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }