1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Computer networks 1: Lecture 9 - Phạm Trần Vũ

23 51 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 23
Dung lượng 288,96 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Lecture Computer networks 1 - Lecture 9: Socket Programming with Java has contents: Using InetAddress, using socket, using serverSocket, client-Server application with UDP, client-Server application with TCP,.... and othere contents.

Trang 1

Computer Networks 1

(Mạng Máy Tính 1)

Lectured by: Dr Phạm Trần Vũ

Trang 2

Lecture 9:

Socket Programming with Java

CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 3

Using InetAddress (1)

 Get local address

import java.net.*;

public class HostInfo {

public static void main(String args[]) {

HostInfo host = new HostInfo();

} catch (UnknownHostException ex) {

System.err.println("Cannot find local host");}

Trang 4

System.out.println(addresses[i]);

} }

catch (UnknownHostException e) {

System.out.println("Could not find

proxy.hcmut.edu.vn"); }

}

}

CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 5

Using Socket (1)

 Kết nối đên 1 số webserver

import java.net.*;

import java.io.*;

public class getSocketInfo {

public static void main(String[] args) {for (int i = 0; i < args.length; i++) {try {

Socket theSocket = new Socket(args[i], 80);

System.out.println("Connected to " +

theSocket.getInetAddress() +

" on port " + theSocket.getPort() +

" from port " +theSocket.getLocalPort() + " of " + theSocket.getLocalAddress());

Trang 7

public class DayTimeServer {

public final static int daytimePort = 5000; public static void main(String[] args) {

Trang 8

Using ServerSocket (2)

while (true) {

theConnection = theServer.accept();

p = new PrintStream(theConnection.getOutputStr eam());

p.println(new Date());

theConnection.close();

} theServer.close();

}catch (IOException e) {

System.err.println(e);

} }

} CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 9

Client-Server Application with UDP

Trang 10

UDP Client (1)

CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 11

UDP Client (2)

Trang 12

UDP Server (1)

CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 13

UDP Server (2)

Trang 14

Client-Server Application with TCP (4)

CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 15

TCP Client (1)

Trang 16

TCP Client (2)

CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 17

TCP Server (1)

Trang 18

TCP Server (2)

CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 19

6. public void run() {

7. // compute primes larger than minPrime

Trang 20

Stop a Thread (1)

 Using Thread.interrupt(), after changing loop condition

 This method does not work with ServerSocket.accept()!

public void stop() {

CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 22

Server Side Thread

Main window thread

•Main Server Form

Grand-child thread

•Handle a connection

•Create a thread to handle socket

InputStream •Handle socket

InputStreamGrand-grand-child

CuuDuongThanCong.com https://fb.com/tailieudientucntt

Trang 23

Client Side Thread

Main window thread

•Main Client Form

•Create a window for

new connection Chat window thread

•Chat window

•Connect to a server socket

•Create a thread to handle socket

InputStream Grand-child thread

•Handle socket InputStream

Ngày đăng: 10/01/2020, 23:48

TỪ KHÓA LIÊN QUAN