高分紧急求问怎么能够让在两台机器上的对应文件夹里的文件能保持一致 1、线程,定时检查2、某台机子改变了文件,然后它通知另一台机子:“我的xx文件夹xx.txt改变了!” 另一台机子过去更新 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个思路我知道,但是我不知道怎么实现。请给个大概的模本。我对socket一敲不同。onefox大牛,可以告诉我吗? 对socket一敲不同。就学者用啊。import java.io.*;import java.net.*;public class SockServerExam { int portnum = 5555; public static void main(String args[]) { new SockServerExam().work(); } void work() { try { ServerSocket server_socket=new ServerSocket(portnum); System.out.println("listen on "+portnum); while(true) { Socket socket = server_socket.accept(); System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort()); RequestHandler rh = new RequestHandler(socket); Thread th = new Thread(rh); th.start(); } }// catch(InterruptedException e)// {// e.printStackTrace();// } catch(IOException eio) { eio.printStackTrace(); } } }class RequestHandler implements Runnable{ Socket conn; InputStream input; OutputStream output; BufferedReader br; byte[] buf; public RequestHandler(Socket s) { try { conn=s; this.input = conn.getInputStream(); this.output = conn.getOutputStream(); buf = new byte[256]; } catch(Exception e) { e.printStackTrace(); } } public void run() { int receive_count=0; int readcount = 1; try { while(readcount>0) { readcount=input.read(buf); System.out.println("received num:"+readcount); if(readcount>0) receive_count+=readcount; } System.out.println("connection accepted " + conn.getInetAddress() + ":" + conn.getPort()+ " received bytes:" + receive_count); input.close(); output.close(); conn.close(); } catch(Exception e) { e.printStackTrace(); } }} import java.nio.*;import java.nio.channels.*;import java.net.*;import java.io.*;import java.nio.channels.spi.*;import java.nio.charset.*;import java.lang.*;public class Client{ public SocketChannel client = null; public InetSocketAddress isa = null; public RecvThread rt = null; public Client() { } public void makeConnection() { int result = 0; try { client = SocketChannel.open(); isa = new InetSocketAddress("dell1",4900); client.connect(isa); client.configureBlocking(false); receiveMessage(); } catch(UnknownHostException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } while ((result = sendMessage()) != -1) { } try { client.close(); System.exit(0); } catch(IOException e) { e.printStackTrace(); } } public int sendMessage() { System.out.println("Inside SendMessage"); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String msg = null; ByteBuffer bytebuf = ByteBuffer.allocate(1024); int nBytes = 0; try { msg = in.readLine(); System.out.println("msg is "+msg); bytebuf = ByteBuffer.wrap(msg.getBytes()); nBytes = client.write(bytebuf); System.out.println("nBytes is "+nBytes); if (msg.equals("quit") || msg.equals("shutdown")) { System.out.println("time to stop the client"); interruptThread(); try { Thread.sleep(5000); } catch(Exception e) { e.printStackTrace(); } client.close(); return -1; } } catch(IOException e) { e.printStackTrace(); } System.out.println("Wrote "+nBytes +" bytes to the server"); return nBytes; } public void receiveMessage() { rt = new RecvThread("Receive THread",client); rt.start(); } public void interruptThread() { rt.val = false; } public static void main(String args[]) { Client cl = new Client(); cl.makeConnection(); } public class RecvThread extends Thread { public SocketChannel sc = null; public boolean val = true; public RecvThread(String str,SocketChannel client) { super(str); sc = client; } public void run() { System.out.println("Inside receivemsg"); int nBytes = 0; ByteBuffer buf = ByteBuffer.allocate(2048); try { while (val) { while ( (nBytes = nBytes = client.read(buf)) > 0){ buf.flip(); Charset charset = Charset.forName("us-ascii"); CharsetDecoder decoder = charset.newDecoder(); CharBuffer charBuffer = decoder.decode(buf); String result = charBuffer.toString(); System.out.println(result); buf.flip(); } } } catch(IOException e) { e.printStackTrace(); } } }} 哇~~! 5 星的老大, 台耀眼了 >_<!! 做这么一个东西干吗?做版本控制吗?浪费时间和精力啊.干吗不用CVS呢? java如何打开一个带有IP地址的文件 关于一些程序运行 好的屏幕取词插件 初学JAVA,想问一个关于servlet和jsp运行的问题 如何获得一个Swing Component相对Screen的绝对Location HTTP回应的两个时间都是什么? 数据库连接的一点小问题? 抛出异常有什么用? 在线等!我看不懂dos提示的错误,请高手看看,谢谢!急急急~! jtable设置可选择不可编辑 如不用线程可以达到改变颜色的目的吗? 怎么在一个函数中使用 静态static 变量,注意:不是指静态类变量
请给个大概的模本。
我对socket一敲不同。
onefox大牛,可以告诉我吗?
import java.io.*;
import java.net.*;public class SockServerExam
{
int portnum = 5555;
public static void main(String args[])
{
new SockServerExam().work();
}
void work()
{
try
{
ServerSocket server_socket=new ServerSocket(portnum);
System.out.println("listen on "+portnum);
while(true)
{
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort());
RequestHandler rh = new RequestHandler(socket);
Thread th = new Thread(rh);
th.start();
}
}
// catch(InterruptedException e)
// {
// e.printStackTrace();
// }
catch(IOException eio)
{
eio.printStackTrace();
}
}
}
class RequestHandler implements Runnable
{
Socket conn;
InputStream input;
OutputStream output;
BufferedReader br;
byte[] buf;
public RequestHandler(Socket s)
{
try
{
conn=s;
this.input = conn.getInputStream();
this.output = conn.getOutputStream();
buf = new byte[256];
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
int receive_count=0;
int readcount = 1;
try
{
while(readcount>0)
{
readcount=input.read(buf);
System.out.println("received num:"+readcount);
if(readcount>0)
receive_count+=readcount;
}
System.out.println("connection accepted " +
conn.getInetAddress() +
":" + conn.getPort()+ " received bytes:" + receive_count);
input.close();
output.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
} }
}
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.io.*;
import java.nio.channels.spi.*;
import java.nio.charset.*;
import java.lang.*;
public class Client
{
public SocketChannel client = null;
public InetSocketAddress isa = null;
public RecvThread rt = null; public Client()
{
}
public void makeConnection()
{
int result = 0;
try
{
client = SocketChannel.open();
isa = new InetSocketAddress("dell1",4900);
client.connect(isa);
client.configureBlocking(false);
receiveMessage();
}
catch(UnknownHostException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
while ((result = sendMessage()) != -1)
{
} try
{
client.close();
System.exit(0);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public int sendMessage()
{
System.out.println("Inside SendMessage");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
ByteBuffer bytebuf = ByteBuffer.allocate(1024);
int nBytes = 0;
try
{
msg = in.readLine();
System.out.println("msg is "+msg);
bytebuf = ByteBuffer.wrap(msg.getBytes());
nBytes = client.write(bytebuf);
System.out.println("nBytes is "+nBytes);
if (msg.equals("quit") || msg.equals("shutdown")) {
System.out.println("time to stop the client");
interruptThread();
try
{
Thread.sleep(5000);
}
catch(Exception e)
{
e.printStackTrace();
}
client.close();
return -1;
}
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("Wrote "+nBytes +" bytes to the server");
return nBytes;
} public void receiveMessage()
{
rt = new RecvThread("Receive THread",client);
rt.start(); } public void interruptThread()
{
rt.val = false;
} public static void main(String args[])
{
Client cl = new Client();
cl.makeConnection();
} public class RecvThread extends Thread
{
public SocketChannel sc = null;
public boolean val = true;
public RecvThread(String str,SocketChannel client)
{
super(str);
sc = client;
}
public void run() { System.out.println("Inside receivemsg");
int nBytes = 0;
ByteBuffer buf = ByteBuffer.allocate(2048);
try
{
while (val)
{
while ( (nBytes = nBytes = client.read(buf)) > 0){
buf.flip();
Charset charset = Charset.forName("us-ascii");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buf);
String result = charBuffer.toString();
System.out.println(result);
buf.flip();
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}