集册 Java实例教程 从服务器套接字读取和写入消息

从服务器套接字读取和写入消息

欢马劈雪     最近更新时间:2020-01-02 10:19:05

520
从服务器套接字读取和写入消息

import java.io.*;

import java.lang.ClassNotFoundException;
/** from 
NowJava.com**/

import java.net.*;

 

public class SocketServer{

    

    private static ServerSocket server;

    private static int port = 3002;


    public static void main(String args[]) throws IOException, ClassNotFoundException{

        server = new ServerSocket(port);

        while(true){

            System.out.println("Waiting for client request");

            Socket socket = server.accept();

            BufferedReader ois = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            /*
            N o w J a v a . c o m
            */

            String message = ois.readLine();

            System.out.println("Message Received: " + message);

            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

            oos.writeObject("Hi Client "+message);

            ois.close();

            oo
展开阅读全文