网络编程应用:基于TCP协议【实现对象传输】--练习
生活随笔
收集整理的這篇文章主要介紹了
网络编程应用:基于TCP协议【实现对象传输】--练习
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
要求:
基于TCP協(xié)議實(shí)現(xiàn),客服端向服務(wù)器發(fā)送一個(gè)對(duì)象 服務(wù)器接受并顯示用戶信息 ,同時(shí)返回給客戶端 "數(shù)據(jù)已收到"建一個(gè)Student類,屬性:name ageStudent類
package Homework3;import java.io.Serializable;public class Student implements Serializable {private static final long serialVersionUID = 1L;private String name;private int age;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";}public Student(String name, int age, String sex) {super();this.name = name;this.age = age;this.sex = sex;}public Student() {super();}}客戶端:
package Homework3;import java.io.IOException; import java.io.InputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.net.Socket;public class Client {public static void main(String[] args) {System.out.println("客戶端已啟動(dòng)!");//1.創(chuàng)建要傳輸?shù)腟tudent對(duì)象Student student=new Student("吳德利", 30, "女");Socket socket=null;OutputStream os=null;InputStream is=null;ObjectOutputStream oos=null;byte[] b=new byte[1024];try {//2.創(chuàng)建Socket對(duì)象,并得到相應(yīng)的輸出流----以及創(chuàng)建對(duì)象流socket=new Socket("localhost", 9999);os=socket.getOutputStream();oos=new ObjectOutputStream(os);//3.將對(duì)象發(fā)送給服務(wù)器oos.writeObject(student);//4.接收服務(wù)器端傳輸完畢,返回的回復(fù)is=socket.getInputStream();is.read(b);String string=new String(b);System.out.println(string);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(os!=null){try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(is!=null){try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket!=null){try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(oos!=null){try {oos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }服務(wù)器端:
package Homework3;import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket;public class Server {public static void main(String[] args) {System.out.println("服務(wù)器端已啟動(dòng)!");ServerSocket serverSocket=null;Socket socket=null;ObjectInputStream ois=null;OutputStream os=null;InputStream is=null;try {//1.創(chuàng)建ServerSocket對(duì)象,獲得Socket對(duì)象以及輸入serverSocket=new ServerSocket(9999);socket=serverSocket.accept();is=socket.getInputStream();//2.創(chuàng)建對(duì)象流ois=new ObjectInputStream(is);//3.接收數(shù)據(jù),并將接收的對(duì)象轉(zhuǎn)為原來的對(duì)象類型Student student=(Student)ois.readObject();System.out.println(student);//4.返回給客戶端,"數(shù)據(jù)已收到"os=socket.getOutputStream();os.write("數(shù)據(jù)已收到".getBytes());os.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(serverSocket!=null){try {serverSocket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket!=null){try {socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(os!=null){try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(is!=null){try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ois!=null){try {ois.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }運(yùn)行結(jié)果:
客戶端:
服務(wù)器端:
總結(jié)
以上是生活随笔為你收集整理的网络编程应用:基于TCP协议【实现对象传输】--练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络编程应用:基于TCP协议【实现文件上
- 下一篇: {网络编程}和{多线程}应用:基于TCP