socket的面试题求解 使用socket,实现从客户端传一文件到服务器端,并在服务器端控制台打印出来,文件为:D:/data.txt 内容为:hello!服务器和客户端都在本地——————————————程序怎么写 请指导下? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先用文件流将文件内容存储在String中,将String转化为byte,通过socket将byte发至服务器并打印 就用Socket,然后做一个BufferedReader跟一个PrintWriter连起来然后就可以随便发送了 就用Socket,然后做一个BufferedReader跟一个PrintWriter连起来然后就可以随便发送了 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("关闭出错");} }} 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("关闭出错");} }} Java做嵌入式好还是做网络方面强??? applet 请教高手用java如何获取系统硬件信息? 不定的日期格式字串有什么简单方法转换为java.sql.date呢 在线等,createImage()为什么返回null? 关于TIMER的问题. java菜鸟求助~这个程序的目的是为了读取一个文件夹中的文件,把at替换成@,找出邮箱并且把所有的邮箱打印到email文件中,但是有两个问题解决不了 代理的应用 如何在打包文件﹐如.jar中帶java的命令行參數﹐如-ms128m JAVA如何调用系统关闭显示器 JAVA的IO问题 如何画2象素的曲线?
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("关闭出错");}
}}
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("关闭出错");}
}}