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());
String data = inFromClient.readUTF(); String[] a = data.split("~");
for (int i = 0; i < a.length; i++) { int son = Integer.parseInt(a[i]); if( son > 0 ){ s += son; } } outFromClient.writeUTF(String.valueOf( (int) s )); socket.close(); serverSocket.close(); inFromClient.close(); outFromClient.close(); if (socket.isClosed()) System.out.println("tcp.Server yopildi"); }
TCP soketi asosida ixtiyoriy matnni teskari tartibda yozish kliyent-server dasturini tuzing. Download 426.5 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling