Download presentation
1
Java 13. Networking public class SumTest {
public static void main(String a1[]) { int a, b, sum; a = Integer.parseInt(a1[0]); b = Integer.parseInt(a1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 System.out.println("두수의 합은 " + sum + "입니다"); }
2
Client/Server Communications
After the server accepts the connection, communication between server and client is conducted the same as for I/O streams. The server must be running when a client starts. The server waits for a connection request from a client. To establish a server, you need to create a server socket and attach it to a port, which is where the server listens for connections. After a server socket is created, the server can use this statement to listen for connections. The client issues this statement to request a connection to a server.
3
Data Transmission through Sockets
InputStream input = socket.getInputStream(); OutputStream output = socket.getOutputStream();
4
A Client/Server Example
Problem: Write a client to send data to a server. The server receives the data, uses it to produce a result, and then sends the result back to the client. The client displays the result on the console. In this example, the data sent from the client is the radius of a circle, and the result produced by the server is the area of the circle.
5
A Client/Server Example, cont.
6
A Client/Server Example, cont.
7
Server.java
8
Client.java
9
Client.java
10
ServerSocket Class - Constructor - Method 형식 Method Description
ServerSocket(int port) throws IOException) 형식 Method Description Socket accept() throws IOException Listens for a connection to be made to this socket and accepts it. void close() throws IOException Closes this socket.
11
Socket Class - Constructor 형식
Socket(String hostName, int port) throws UnknownHostException, IOException 형식
12
Method Method Description
OutputStream getOutputStream( ) throws IOException Returns an output stream for this socket. void close( ) throws IOException Closes this socket. InetAddress getInetAddress( ) Returns the address to which the socket is connected. InetAddress getLocalAddress( ) Gets the local address to which the socket is bound. int getPort( ) Returns the remote port number to which this socket is connected. int getLocalPort( ) Returns the local port number to which this socket is bound. InputStream getInputStream( ) throws IOException Returns an input stream for this socket.
13
TCP Socket Network Network Client Server Client Server
14
Excercises ServerSide.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14
15 16 17 18 19 import java.io.*; import java.net.*; class ServerSide { public static void main(String args[]) throws Exception { int port = Integer.parseInt(args[0]); int times = Integer.parseInt(args[1]); ServerSocket ss = new ServerSocket(port); int i = 1; while( i <= times) { Socket s = ss.accept(); OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); for(int j = 1 ; j <= 10 ; j++) dos.writeInt(j); s.close(); ++i; }
15
Excercises ClientSide.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14
15 16 17 import java.io.*; import java.net.*; public class ClientSide { public static void main(String args[]) throws Exception { String server = args[0]; int port = Integer.parseInt(args[1]); Socket c = new Socket(server, port); InputStream is = c.getInputStream(); DataInputStream dis = new DataInputStream(is); for(int i=1 ; i <= 10 ; i++) { int j = dis.readInt(); System.out.println (j); } c.close();
17
SimpleServer.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 import java.io.*; import java.net.*; public class SimpleServer { public static void main(String[] arsg) { ServerSocket s=null; try { s = new ServerSocket(5432); } catch (IOException e) { e.printStackTrace(); } System.out.println("Server Running.. Waiting Connetion .."); while (true) { try { Socket s1 = s.accept(); OutputStream s1out = s1.getOutputStream(); BufferedWriterbw = new BufferedWriter( new OutputStreamWriter(s1out)); bw.write("Server Message\n"); bw.close(); s1.close(); }
18
SimpleServer.java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 import java.io.*; import java.net.*; public class SimpleClient { public static void main(String[] args) { try { Socket s1 = new Socket(" ", 5432); InputStreamir = s1.getInputStream(); BufferedReaderbr = new BufferedReader(new InputStreamReader(ir)); int a = 0; while((a = br.read())!= -1){ System.out.print((char)a); } ir.close(); s1.close(); catch (ConnectExceptionconnExc) { System.err.println("Could not connect ro the server"); catch (IOException e) {
19
Server Message
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.