|
U will need at least 3 classes: Server that listens port for connection and creates ServerConnections when clients sends files. And Client. Here is the core of these clasees:
import java.net.*;
import java.io.*;
public class Server implements Runnable {
public static void main(String[] args) {
new Thread(new ServerLogger(out)).start();
}
private Server() {
conCount=0;
}
public void run() {
try {
ServerSocket acceptSocket=new ServerSocket(Config.port);
while (true) {
try {
Socket sock=acceptSocket.accept();
new ServerConnection(this, sock);
} catch(Exception ex) {
ex.printStackTrace();
}
}
} catch(Exception e) {
if (!e.toString().startsWith("java.net.SocketExceptio n")) {
e.printStackTrace();
}
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;
public class ServerConnection implements Runnable {
private BufferedReader in;
public ServerConnection(Socket sock) {
try {
this.serverLogger=serverLogger;
sock.setSoTimeout(30000);
in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
new Thread(this).start();
} catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
String s=in.readLine();
if (s != null) {
System.out.println(s);
} else {
return;
}
} catch(Exception e) {
if ((!e.toString().startsWith("java.net.SocketExcepti on")) &&
(!(e instanceof SocketTimeoutException))) {
e.printStackTrace();
} else {
return;
}
}
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class Client implements {
private PrintWriter out;
public static void main(String[] args) {
new Logger();
}
public Client() {
try {
Socket s=new Socket(Config.serversite, Config.serverport);
out=new PrintWriter(s.getOutputStream(), true);
//write file to out here
} catch(Exception e) {
e.printStackTrace();
}
}
}
|