1. 程式人生 > >Java網絡編程客戶端和服務器通信

Java網絡編程客戶端和服務器通信

nts cat out log auto println args etag sta

在java網絡編程中,客戶端和服務器的通信例子:

先來服務器監聽的代碼

package com.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.jim.Student;

public class QQServer {

    /**
     * @param args
     */
    public
static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Start Server..."); QQServer server = new QQServer(); } //構造函數 public QQServer() { Socket s; //創建ServerSocket在9999號端口監聽 ServerSocket ss = null;
try { ss = new ServerSocket(9999); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("服務器正在監聽........"); try { System.out.println("開始等待連接..."); s
= ss.accept();//阻塞,等待連接 System.out.println("客戶端連接成功"); //接收流 ObjectInputStream ois=new ObjectInputStream(s.getInputStream()); //發送流 ObjectOutputStream oos = new ObjectOutputStream( s.getOutputStream()); //接收Student對象 Student stu = (Student) ois.readObject(); System.out.println(stu.getName()); //發送Student對象 oos.writeObject(new Student("Server")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

客戶端的代碼:

package com.client;

import java.io.*;
import java.net.*;

import com.jim.Student;

public class QQClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Start QQClient...");
        try {
            QQClient client = new QQClient();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //構造函數
    public QQClient() throws IOException
    {
        //String address="127.0.0.1";
        //int port = 9999 ;
        try {
            Socket s = new Socket("127.0.0.1", 9999);
            Student stu = new Student("Jim");
            //發送信息給服務器
            ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
            
            
            //ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
            //接受服務器傳來的信息
            ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
            
            //發送對象到服務器
            //oos.writeObject(new Student("OKK"));
            oos.writeObject(stu);
            
            //接收服務器返回的對象
            Student re_stu = (Student)ois.readObject();
            System.out.println(re_stu.getName());
            
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}

在客戶端和服務傳輸的對象對象

package com.jim;

public class Student implements java.io.Serializable{
    
    private String name ;
    private int age;
    private String address;
    
    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 getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    
    /**
     * @param name
     * 構造方法
     */
    public Student(String name){
        this.name = name;
    }
    

    /**
     * @param 
     * 
     */
    public void study(){
        System.out.println(this.name + " is studing");
    }
    
}

Java網絡編程客戶端和服務器通信