meagan
Uploaded by
12 SLIDES
267 VUES
120LIKES

Java Network Programming: ServerSocket and Object Communication

DESCRIPTION

This guide explores Java Network Programming using ServerSocket and Client-Server models. It covers how to create a server using ServerSocket to listen for client connections on a specified port, and how to exchange messages between the server and client applications. The practical examples include creating Client1.java and Server1.java programs to communicate through specified ports, implementing a simple protocol for greeting messages, and handling object transmission over networks using ObjectOutputStream and ObjectInputStream. Exercises are provided for hands-on learning.

1 / 12

Télécharger la présentation

Java Network Programming: ServerSocket and Object Communication

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Network Programming 3 352362 – Network Operating Systems and Protocols ChoopanRattanapoka

  2. Java ServerSocket Server Server มีหน้าที่รอการติดต่อจาก Client ใน port ที่ตัวเองจะให้บริการ ใน Java จะสามารถทำได้โดยการสร้าง Object ของ ServerSocket ตัวอย่าง : ServerSocketserver = new ServerSocket(หมายเลข port);

  3. Client and Server Client Server 192.168.1.1 ServerSocket server = new ServerSocket (2000); Socket s = new Socket (“192.168.1.1”, 2000); ฝ่าย Server จำเป็นจะต้องมี Socket เพื่อติดต่อกับ Client สามารถทำได้โดย Socket client = server.accept();

  4. Example 1

  5. Exercise 1 • จงเขียนโปรแกรม Client1.java และ Server1.java โดย • Server1.java • เป็นโปรแกรม Server ที่จะให้บริการที่พอร์ต 4567 • Client1.java • เป็นโปรแกรม Client ที่จะติดต่อไปยัง server ที่พอร์ต 4567 • Protocol • Client จะเป็นฝ่ายส่งคำว่า “Hello” ไปหา Server • Server จะตอบว่า “Welcome” • ถ้า Client ส่งหาอื่นไปหา Server, Server จะตอบ “GO AWAY” • Client จะแสดงค่าที่ server ตอบกลับพร้อมทั้งปิดการเชื่อมต่อ

  6. การส่ง Java Object ผ่านเครือข่าย • ใช้ Class ช่วยคือ ObjectOutputStream • Constructor • public ObjectOutputStream(OutputStream out) throws IOException • Method • void writeObject(Objectobj) • void write(byte[] buf) •  void writeInt(intval)

  7. การรับ Java Object ผ่านเครือข่าย • ใช้ Class ช่วยคือ ObjectInputStream • Constructor • public ObjectInputStream(InputStreamin) throws IOException • Method • Object readObject() • intread(byte[] buf, int off, intlen) • intreadInt()

  8. Example : MyObject.java

  9. Example : ObjectServer.java

  10. Example : ObjectClient.java (1)

  11. Example : ObjectClient.java (2)

  12. Exercise 2 • ให้เขียนโปรแกรม Client.javaที่จะรับค่าคะแนนสอบ(ตัวเลข) จาก command line เช่น java Client 75 • โปรแกรมจะส่งตัวเลขนั้นไปยัง Server ผ่าน Object ที่ชื่อ Grade • แล้วโปรแกรมจะรับ Object กลับจาก Server พร้อมทั้งแสดงเกรดที่ได้ • ให้เขียนโปรแกรม Server.java • รอรับ Object Grade จาก Client • คำนวณเกรด >= 80 (A), >= 70 (B), >=60 (C) , >= 50 (D) , F ( 0 – 49) แล้วค่ากลับไปให้กลับ Client

More Related