13. networking
DESCRIPTION
13. Networking. Java. 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 + " 입니다 "); } }. - PowerPoint PPT PresentationTRANSCRIPT
13. Networking
1
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 + " 입니다 "); }}
JAVA
2
Client/Server Communica-tions
Server Host
Server socket on port 8000 SeverSocket server = new ServerSocket(8000);
A client socket Socket socket = server.accept()
Client Host
Client socket Socket socket = new Socket(host, 8000)
I/O Stream
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.
After the server accepts the connection, communication between server and client is conducted the same as for I/O streams.
3
Data Transmission through Sockets
int port = 8000;DataInputStream in;DataOutputStream out;ServerSocket server;Socket socket;
server =new ServerSocket(port);socket=server.accept();in=new DataInputStream (socket.getInputStream());out=new DataOutStream (socket.getOutputStream());System.out.println(in.readDouble());out.writeDouble(aNumber);
int port = 8000;String host="localhost"DataInputStream in;DataOutputStream out;Socket socket;
socket=new Socket(host, port);in=new DataInputStream (socket.getInputStream());out=new DataOutputStream (socket.getOutputStream());out.writeDouble(aNumber);System.out.println(in.readDouble());
ClientServer
ConnectionRequest
I/OStreams
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 re-sult on the console. In this example, the data sent from the client is the radius of a circle, and the result produced bythe server is the area of the circle.
radius
Server Client
compute area
area
5
A Client/Server Example, cont.
Server
radius
DataInputStream
socket.getInputStream
socket
Network
Client
radius
DataOutputStream
socket.getOutputStream
socket
(A)
Server
area
DataOutputStream
socket.getOutputStream
socket
Network
Client
area
DataOutputStream
socket.getOutputStream
socket
(B)
6
A Client/Server Example, cont.
radius
Server Client
compute area
area
7
Server.java
8
Client.java
9
Client.java
13장 . 네트워킹
10
ServerSocket Class
- Constructor
- Method
ServerSocket(int port) throws IOException)형식
Method Description
Socket accept() throws IOExceptionListens for a connection to be made to this socket and accepts it.
void close() throws IOException Closes this socket.
13장 . 네트워킹
11
Socket Class
- Constructor
Socket(String hostName, int port) throws UnknownHostException, IOExcep-tion
형식
13장 . 네트워킹 12
Method
Method DescriptionOutputStream 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 IOEx-ception
Returns an input stream for this socket.
13장 . 네트워킹
13
TCP Socket
Network
Network
Client ClientServer Server
13장 . 네트워킹
14
ExcercisesServerSide.java
01020304050607080910111213141516171819
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; } } }
13장 . 네트워킹
15
ExcercisesClientSide.java
0102030405060708091011121314151617
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(); }}
13장 . 네트워킹
16
13장 . 네트워킹
17
SimpleServer.java
0102030405060708091011121314151617181920
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();
}catch (IOException e) { e.printStackTrace(); }
}}
}
13장 . 네트워킹
18
SimpleServer.java
0102030405060708091011121314151617181920
import java.io.*;import java.net.*;public class SimpleClient {
public static void main(String[] args) {try {
Socket s1 = new Socket("127.0.0.1", 5432);InputStreamir = s1.getInputStream();
BufferedReaderbr = new BufferedReader(new InputStream-Reader(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) {}
}}
13장 . 네트워킹
19
Server Message