标题:基于套接字Socket通信的编程
只看楼主
wsspi
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2009-2-26
结帖率:100%
 问题点数:0 回复次数:1 
基于套接字Socket通信的编程
编写一个时间服务器/客户程序,当服务器收到客户端请求后,将当前的系统时间以一定的格式发送给客户端,客户端解析这个格式,并以可读的格式在标准输出设备上打印接收到的时间。


谁会呀?我是初学者,大侠帮帮忙!
搜索更多相关主题的帖子: 套接字 通信 Socket 
2009-11-01 09:15
qq342991688
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:106
注 册:2010-5-30
得分:0 
import *;
import *;

public class Client {
    public static void main(String[]args){
        String s=null;
        Socket socket;
        DataInputStream in=null;
        DataOutputStream out=null;
        try{
            socket=new Socket("localhost",4331);
            in=new DataInputStream(socket.getInputStream());
            out=new DataOutputStream(socket.getOutputStream());
            out.writeUTF("hello! It is Client");
            while(true){
                s=in.readUTF();
                out.writeUTF(":"+Math.random());
                System.out.println("client get****:"+s);
                Thread.sleep(2000);
            }
        }catch(IOException e){
            System.out.println("can't connect");
            
        }catch(InterruptedException e){}
    }

}



package awt;

import *;
import *;

public class Serever {
    public static void main(String [] args){
        ServerSocket server=null;
        Socket socket=null;
        String s=null;
        DataOutputStream out=null;
        DataInputStream in=null;
        try{
            server=new ServerSocket(4331);
        }catch(IOException e1){
            System.out.println("ERROR:"+e1);
        }try{
            socket=server.accept();
            in=new DataInputStream(socket.getInputStream());
            out=new DataOutputStream(socket.getOutputStream());
            while(true){
                s=in.readUTF();
                out.writeUTF("It's Server:");
                out.writeUTF("client sends:"+s);
                Thread.sleep(2000);
            }
        }catch(IOException e){
            System.out.println(""+e);
        }catch(InterruptedException e){}
    }

}

自己改一下
2010-06-12 16:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-290642-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.521445 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved