Программирование на Java

       

30 hops max, 38 byte


traceroute to netserv1.chg.ru (193.233.46.3), 30 hops max, 38 byte packets 1 n3-core.mipt.ru (194.85.80.1) 1.508 ms 0.617 ms 0.798 ms 2 mipt-gw-eth0.mipt.ru (193.125.142.177) 2.362 ms 2.666 ms 1.449 ms 3 msu-mipt-atm0.mipt.ru (212.16.1.1) 5.536 ms 5.993 ms 10.431 ms 4 M9-LYNX.ATM6-0.11.M9-R2.msu.net (193.232.127.229) 12.994 ms 7.830 ms 6.816 ms 5 Moscow-BNS045-ATM4-0-3.free.net (147.45.20.37) 12.228 ms 7.041 ms 8.731 ms 6 ChgNet-gw.free.net (147.45.20.222) 77.103 ms 75.234 ms 92.334 ms 7 netserv1.chg.ru (193.233.46.3) 96.627 ms 94.714 ms 134.676 ms
Пример 16.1.
Закрыть окно


import java.io.*; import java.net.*;

public class NetServer { public static final int PORT = 2500; private static final int TIME_SEND_SLEEP = 100; private static final int COUNT_TO_SEND = 10; private ServerSocket servSocket;

public static void main(String[] args) { NetServer server = new NetServer(); server.go(); }

public NetServer() { try{ servSocket = new ServerSocket(PORT); } catch(IOException e) { System.err.println("Unable to open Server Socket : " + e.toString()); } }

public void go() {

// Класс-поток для работы с //подключившимся клиентом class Listener implements Runnable { Socket socket; public Listener(Socket aSocket) { socket = aSocket; } public void run() { try { System.out.println("Listener started"); int count = 0; OutputStream out = socket.getOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out); PrintWriter pWriter = new PrintWriter(writer); while (count<COUNT_TO_SEND) { count++; pWriter.print(((count>1)?",":"")+ "Say" + count); sleeps(TIME_SEND_SLEEP); } pWriter.close(); } catch(IOException e) { System.err.println("Exception : " + e.toString()); } } }

// Основной поток, циклически выполняющий метод accept() System.out.println("Server started"); while (true) { try { Socket socket = servSocket.accept(); Listener listener = new Listener(socket); Thread thread = new Thread(listener); thread.start(); } catch(IOException e) { System.err.println("IOException : " + e.toString()); } } }

public void sleeps(long time) { try { Thread.sleep(time); } catch(InterruptedException e) { } } }
Пример 16.2.
Закрыть окно




import java.io.*; import java.net.*;

public class NetClient implements Runnable { public static final int PORT = 2500; public static final String HOST = "localhost"; public static final int CLIENTS_COUNT = 5; public static final int READ_BUFFER_SIZE = 10;

private String name = null;

public static void main(String[] args) { String name = "name"; for (int i=1; i<=CLIENTS_COUNT; i++) { NetClient client = new NetClient(name+i); Thread thread = new Thread(client); thread.start(); } }

public NetClient(String name) { this.name = name; }

public void run() { char[] readed = new char[READ_BUFFER_SIZE]; StringBuffer strBuff = new StringBuffer(); try { Socket socket = new Socket(HOST, PORT); InputStream in = socket.getInputStream(); InputStreamReader reader = new InputStreamReader(in); while (true) { int count = reader.read(readed, 0, READ_BUFFER_SIZE); if (count==-1) break; strBuff.append(readed, 0, count); Thread.yield(); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("client " + name + " read : " + strBuff.toString()); } }
Пример 16.3.
Закрыть окно


import java.io.*; import java.net.*;

public class DatagramDemoServer { public static final int PORT = 2000; private static final int LENGTH_RECEIVE = 1; private static final byte[] answer = ("received").getBytes(); private DatagramSocket servSocket = null; private boolean keepRunning = true; public static void main(String[] args) { DatagramDemoServer server = new DatagramDemoServer(); server.service(); }

public DatagramDemoServer() { try { servSocket = new DatagramSocket(PORT); } catch(SocketException e) { System.err.println("Unable to open socket : " + e.toString()); } } protected void service() { DatagramPacket datagram; InetAddress clientAddr; int clientPort; byte[] data; while (keepRunning) { try { data = new byte[LENGTH_RECEIVE]; datagram = new DatagramPacket(data, data.length); servSocket.receive(datagram); clientAddr = datagram.getAddress(); clientPort = datagram.getPort(); data = getSendData(datagram.getData()); datagram = new DatagramPacket(data, data.length, clientAddr, clientPort); servSocket.send(datagram); } catch(IOException e) { System.err.println("I/O Exception : " + e.toString()); } } } protected byte[] getSendData(byte b[]) { byte[] result = new byte[b.length+answer.length]; System.arraycopy(b, 0, result, 0, b.length); System.arraycopy(answer, 0, result, b.length, answer.length); return result; } }
Пример 16.4.
Закрыть окно

Содержание раздела