平台要求:
3. Windows或Unix/Linux
4. JAVA/VC/BCB/Delphi功能要求:
4. 通过SOCKET实现点对点的文件交换功能
5. 支持连接握手、登录校验、文件传输请求应答、出错重传等交互的通信协议机制
6. 提供简单的用户界面或命令行界
3. Windows或Unix/Linux
4. JAVA/VC/BCB/Delphi功能要求:
4. 通过SOCKET实现点对点的文件交换功能
5. 支持连接握手、登录校验、文件传输请求应答、出错重传等交互的通信协议机制
6. 提供简单的用户界面或命令行界
which one do u really need?
import java.net.Socket;public abstract class TCP implements Runnable {
protected Socket s = null;
protected InputStream in = null;
protected OutputStream out = null;
protected int port = 9999;
private Thread thread = null;
private InputStream standardIn = System.in;
private PrintStream standardOut = System.out;
private boolean connectionClosed = false;
public abstract void initialize(); public void startCommunicate() {
initialize();
thread = new Thread(this);
thread.start();
sendMessage();
}
public void run() {
receiveMessage();
try {
in.close();
standardOut.print("\nCommunication is over");
standardIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void close() {
try {
out.close(); try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (s != null)
s.close();
} catch (IOException e) {
e.printStackTrace();
}
} private String getMessage() {
int ch;
StringBuilder str = new StringBuilder();
try {
int length = in.read();
if (length == -1) return null;
while (length > 0) {
ch = in.read();
str.append((char)ch);
length--;
}
} catch (IOException e) {
//e.printStackTrace();
standardOut.print("...");
}
return str.toString();
}
public void receiveMessage() {
String message = null;
while (!connectionClosed && (message = getMessage()) != null) {
if (message.length() > 0) {
standardOut.print(":>");
standardOut.println(message);
}
}
} public void sendMessage() {
int ch;
StringBuilder str = new StringBuilder();
standardOut.print("\r# ");
try {
while ((ch = standardIn.read()) != -1) {
if (ch != 13 && ch != 10)
str.append((char)ch);
else if (ch == 13)
{
out.write(str.length());
for (int i = 0; i < str.length(); i++)
out.write(str.charAt(i));
str = new StringBuilder();
standardOut.print("# ");
}
}
connectionClosed = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}package messager;import java.net.*;
import java.io.*;public class TCPClient extends TCP {
private InetAddress serverAddress = null;
public TCPClient(int port) {
try {
this.serverAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.port = port;
}
public TCPClient(String serverHost, int port) {
try {
this.serverAddress = InetAddress.getByName(serverHost);
} catch (UnknownHostException e) {
e.printStackTrace();
}
this.port = port;
}
public void initialize() {
Socket s;
try {
s = new Socket(serverAddress, port);
in = s.getInputStream();
out = s.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
String ip = null;
int port = 9999;
if (args.length == 2) {
ip = args[0];
port = Integer.parseInt(args[1]);
}
else if (args.length == 1) {
ip = args[0];
}
TCPClient client = null;
if (ip != null)
client = new TCPClient(ip, port);
else
client = new TCPClient(port);
client.startCommunicate();
client.close();
}
}
package messager;import java.net.*;
import java.io.*;public class TCPServer extends TCP{
private ServerSocket ss = null;
public TCPServer(int port) {
this.port = port;
}
public void initialize() {
try {
ss = new ServerSocket(port);
s = ss.accept();
in = s.getInputStream();
out = s.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
super.close();
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
int port = 9999;
if (args.length > 0)
port = Integer.parseInt(args[0]);
TCPServer server = new TCPServer(port);
server.startCommunicate();
server.close();
}
}