当前位置: 代码迷 >> Java相关 >> 大家看看怎么才能使Netclock.java发送中文
  详细解决方案

大家看看怎么才能使Netclock.java发送中文

热度:8519   发布时间:2013-02-25 21:51:15.0
大家看看如何才能使Netclock.java发送中文

Java code
//返回连接时间的服务器程序  Netclock.java//此程序端口号为6000的服务器程序//收到客户端连接请求后将返回连接时间//安<Ctrl>+C可终止程序的运行//使用方法:java Netclock//package test;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.Date;import javax.xml.crypto.Data;public class Netclock{    /**     * @param args     */    public static void main(String[] args)    {        ServerSocket servsock = null;   //服务器Socket(套接字)        Socket sock;               //读写Socket对象        OutputStream out;         //输出流        String outstr;             //存放输出数据的字符串        int i;                    //输出循环控制变量        Date d;                  //日期对象        try        {            //生成服务器socket            servsock =  new ServerSocket(6000,300);            //服务器处理循环            while(true)            {                sock = servsock.accept();   //接收连接                System.out.println("连接已经受理!");                                //生成输出数据                d = new Date();                outstr = "\n"+"Hello FengTaiyun,can you receive me?"                             +"\n"+d.toString()+"\n"                             +"Yes you have received completely!"+"\n";                //输出数据                                out = sock.getOutputStream();                //发送数据                System.out.println("发送数据\""+outstr+"\"");                for(int j=0; j<outstr.length();j++)                    out.write((int)outstr.charAt(j));                out.write('\n');                                //连接结束                sock.close();            }        }        catch(Exception e)        {            System.exit(1);        }    }}


Java code
//返回连接时间的服务器程序  Netclock.java//此程序端口号为6000的服务器程序//收到客户端连接请求后将返回连接时间//安<Ctrl>+C可终止程序的运行//使用方法:java Netclock//package test;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.Date;import javax.xml.crypto.Data;public class Netclock{    /**     * @param args     */    public static void main(String[] args)    {        ServerSocket servsock = null;   //服务器Socket(套接字)        Socket sock;               //读写Socket对象        OutputStream out;         //输出流        String outstr;             //存放输出数据的字符串        int i;                    //输出循环控制变量        Date d;                  //日期对象        try        {            //生成服务器socket            servsock =  new ServerSocket(6000,300);            //服务器处理循环            while(true)            {                sock = servsock.accept();   //接收连接                System.out.println("连接已经受理!");                                //生成输出数据                d = new Date();                outstr = "\n"+"Hello FengTaiyun,can you receive me?"                             +"\n"+d.toString()+"\n"                             +"Yes you have received completely!"+"\n";                //输出数据                                out = sock.getOutputStream();                //发送数据                System.out.println("发送数据\""+outstr+"\"");                for(int j=0; j<outstr.length();j++)                    out.write((int)outstr.charAt(j));                out.write('\n');                                //连接结束                sock.close();            }        }        catch(Exception e)        {            System.exit(1);        }    }}




------解决方案--------------------------------------------------------
你没有帖客户端的代码,我给帮你写了个客户端的代码
我都调试通过了,程序正常运行
Java code
//返回连接时间的服务器程序 Netclock.java //此程序端口号为6000的服务器程序 //收到客户端连接请求后将返回连接时间 //安<Ctrl>+C可终止程序的运行 //使用方法:java Netclock package test;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.Date;import java.io.PrintWriter;import javax.xml.crypto.Data;public class Netclock{    /**     * @param args     */    public static void main(String[] args)    {        ServerSocket servsock = null;   //服务器Socket(套接字)        Socket sock;               //读写Socket对象        OutputStream out;         //输出流        String outstr;             //存放输出数据的字符串        int i;                    //输出循环控制变量        Date d;                  //日期对象        try        {            //生成服务器socket            servsock =  new ServerSocket(6000);            //服务器处理循环            while(true)            {                sock = servsock.accept();   //接收连接                System.out.println("连接已经受理!");                                //生成输出数据                d = new Date();                outstr = "\n"+"Hello FengTaiyun,can you receive me?"                             +"\n"+d.toString()+"\n"                             +"Yes you have received completely!"+"\n";                //输出数据                                out = sock.getOutputStream();                PrintWriter pw=new PrintWriter(out);                //发送数据                System.out.println("发送数据\""+outstr+"\"");                pw.write(outstr);                //连接结束                out.close();                pw.close();                sock.close();            }        }        catch(Exception e)        {            System.exit(1);        }    }}//客户端测试时间服务器package test;import java.net.*;import java.util.*;import java.io.*;public class LockClient{       public static void main(String[] args)       {        int port = 6000;        Socket s = getSocket(port);                                          try        {           System.out.println("Connected on port " + port);           BufferedReader in =new BufferedReader(new InputStreamReader(s.getInputStream()));                                    PrintWriter out;            out = new PrintWriter(s.getOutputStream(),true);             String quote = in.readLine();                                // write the quote on the chalkboard            while(quote!=null)       {                                           System.out.println(quote);                quote = in.readLine();             }            s.close();              out.close();            in.close();        }        catch (Exception e)        {            e.printStackTrace();                                                  }       }       private static Socket getSocket(int port)                                 {        Socket s=null;        String host;        InetAddress ip;                                         try            {                //ip = InetAddress.getByName(host);                s=new Socket("127.0.0.1",port);                return s;                                                            }            catch (UnknownHostException e)                                            {                System.out.println("The host is unknown.");                            }            catch (IOException e)            {                System.out.println("Network error.");            }            finally{                            }            return s;       }}
  相关解决方案