Tarmoqni dasturlash asoslari
Download 426.5 Kb.
|
TDA
- Bu sahifa navigatsiya:
- TCPServer.java
TCPClient.java
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.Scanner; public class TCPClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost", 8080); DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); DataInputStream inputStream = new DataInputStream(socket.getInputStream()); Scanner scanner = new Scanner(System.in); System.out.print("Enter n: "); int n = scanner.nextInt(); StringBuilder data = new StringBuilder(); for (int i = 0; i < n; i++) { data.append(scanner.nextInt()).append("~"); } outputStream.writeUTF(data.toString()); String result = inputStream.readUTF();
socket.close(); inputStream.close(); outputStream.close(); }
TCPServer.java import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Arrays; public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8080); Socket socket = serverSocket.accept(); if (socket.isConnected()) { System.out.println("tcp.Server ishga tushdi"); } DataInputStream inFromClient = new DataInputStream(socket.getInputStream()); DataOutputStream outFromClient = new DataOutputStream(socket.getOutputStream()); String data = inFromClient.readUTF(); String[] array = data.split("~"); Arrays.sort(array); StringBuilder result = new StringBuilder(); for (int i = 0; i < array.length; i++) { result.append(array[i]).append(" "); } outFromClient.writeUTF(String.valueOf((result))); socket.close(); serverSocket.close(); inFromClient.close(); outFromClient.close(); if (socket.isClosed()) { System.out.println("tcp.Server yopildi"); } }
TCP soketi asosida ixtiyoriy n ta sondan musbatlari yig‘indisini hisoblash kliyent-server dasturini tuzing. Download 426.5 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling