您的位置:程序门 -> java -> j2se / 基础类



socket的面试题求解


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


socket的面试题求解[已结贴,结贴人:zxlion]
发表于:2007-03-05 17:58:03 楼主
使用socket,实现从客户端传一文件到服务器端,并在服务器端控制台打印出来,文件为:d:/data.txt   内容为:hello!
服务器和客户端都在本地
——————————————
程序怎么写   请指导下?
发表于:2007-03-05 18:44:241楼 得分:0
先用文件流将文件内容存储在string中,将string转化为byte,通过socket将byte发至服务器并打印
发表于:2007-03-05 21:11:322楼 得分:0
就用socket,然后做一个bufferedreader跟一个printwriter连起来然后就可以随便发送了
发表于:2007-03-05 21:12:083楼 得分:0
就用socket,然后做一个bufferedreader跟一个printwriter连起来然后就可以随便发送了
发表于:2007-03-06 09:07:254楼 得分:0
具体代码怎么写呢?
发表于:2007-03-08 18:07:475楼 得分:10
import   java.net.*;
import   java.io.*;
public   class   server
{
private   serversocket   server;
private   socket   you;
private   receive   rec;
public   server()
{
try
{
server   =   new   serversocket(2007);
    system.out.println( "服务器运行中,监听端口:2007...... ");
while(true)
{
you   =   server.accept();
if(you   !=   null)
{
system.out.println( "有客户连接启动接收线程... ");
new   thread(new   receive(you)).start();
system.out.println( "接收文件内容如下:   ---> 服务器继续监听中... ");
}
}
}catch(exception   e){system.out.println( "服务端运行出错! ");}
}

public   static   void   main(string   args[])
{
new   server();
}
}

class   receive   implements   runnable
{
private   file   files;
private   datainputstream   din   =   null;
private   dataoutputstream   dout   =   null;
private   socket   mysock   =   null;
private   int   str   =   1;
public   receive(socket   you)
{
this.mysock   =   you;
//files   =   new   file( "d:\\data.txt ");
}

public   void   run()
{
try
{
din   =   new   datainputstream(mysock.getinputstream());
dout   =   new   dataoutputstream(mysock.getoutputstream());

while(true)
{
if((str=din.readint())   ==   0)
break;
system.out.println( " "   +   din.readutf());
}
clears();
}catch(exception   e){system.out.println( "接收出错! ");}
finally
{
clears();
}

}

void   clears()
{
try
{
dout.close();
din.close();
mysock.close();
}catch(exception   e){system.out.println( "关闭出错 ");}
    }

}
发表于:2007-03-08 18:10:206楼 得分:10
import   java.net.*;
import   java.io.*;
public   class   client
{
private   socket   you;
senddata   sends;

public   client()
{
try
{      
                      you   =   new   socket( "localhost ",2007);
  system.out.println( "连接服务器监听端口:2007...... ");
  if(you   !=   null)
  {
        system.out.println( "连接成功,启动发送线程... ");
        new   thread(new   senddata(you)).start();
        system.out.println( "发送完毕!关闭线程... ");
    }
}catch(exception   e){system.out.println( "服务端运行出错! ");}
}

public   static   void   main(string   args[])
{
new   client();
}
}

class   senddata   implements   runnable
{
private   file   files;
private   bufferedreader   bin   =   null;
private   datainputstream   din   =   null;
private   dataoutputstream   dout   =   null;
private   socket   mysock   =   null;
private   string   data   =   " ";
public   senddata(socket   you)
{
this.mysock   =   you;
files   =   new   file( "d:\\data.txt ");
}

public   void   run()
{
try
{
bin   =   new   bufferedreader(new   inputstreamreader(new   fileinputstream(files)));
din   =   new   datainputstream(mysock.getinputstream());
dout   =   new   dataoutputstream(mysock.getoutputstream());
while((data   =   bin.readline())   !=   null)
{
dout.writeint(1);
dout.writeutf(data);
}
dout.writeint(0);
clears();
}catch(exception   e){}
finally
{
clears();
}

}

void   clears()
{
try
{
bin.close();
dout.close();
din.close();
mysock.close();
}catch(exception   e){system.out.println( "关闭出错 ");}
    }

}
发表于:2007-03-08 21:41:017楼 得分:0
learnning


快速检索

最新资讯
热门点击