Java网络编程客户端和服务器通信

在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");
    }
    
}

 

posted @ 2017-10-14 17:22  Blue眼泪2016  阅读(2745)  评论(0编辑  收藏  举报