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

Posted Blue眼泪2016

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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");
    }
    
}

 

以上是关于Java网络编程客户端和服务器通信的主要内容,如果未能解决你的问题,请参考以下文章

Java编程基础之网络编程

Java网络编程 - TCP通信

java网络多线程专题

java中socket网络通信,实现客户端上传文件案例

java socket实现服务端,客户端简单网络通信。Chat

Java 网络编程案例一:一台客户端与服务器单次通信