import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
public class netCopyServer { public static char[] a; public static void main(String[] args) throws IOException {
ServerSocket ssock = new ServerSocket(2222);
System.out.println("Listening...");
Socket sock = ssock.accept();
FileInputStream fs = new FileInputStream("dd.txt");
OutputStream os = sock.getOutputStream();
byte[] b = new byte[255];
int byteread = 0;
while ((byteread = fs.read(b)) != -1) {
os.write(b, 0, byteread);
}
os.flush();
os.close();
sock.close(); }
}
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;public class netCopyClient {
public static void main(String[] args) throws IOException, UnknownHostException {
Socket sock = new Socket("127.0.0.1", 2222);
FileOutputStream fos = new FileOutputStream("bbs.txt");
InputStream is = sock.getInputStream();
byte[] b = new byte[255];
int byteread = 0;
while ((byteread = is.read(b)) != -1) {
fos.write(b, 0, byteread);
}
fos.flush();
fos.close();
sock.close();
}
}
客户端都会了,服务器端怎么就写错了呢????
import java.net.Socket;
import java.io.*;
public class netCopyServer { public static char[] a; public static void main(String[] args) throws IOException {
ServerSocket ssock = new ServerSocket(2222);
System.out.println("Listening...");
Socket sock = ssock.accept();
FileInputStream fs = new FileInputStream("dd.txt");
OutputStream os = sock.getOutputStream();
byte[] b = new byte[255];
int byteread = 0;
while ((byteread = fs.read(b)) != -1) {
os.write(b, 0, byteread);
}
os.flush();
os.close();
sock.close(); }
}
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;public class netCopyClient {
public static void main(String[] args) throws IOException, UnknownHostException {
Socket sock = new Socket("127.0.0.1", 2222);
FileOutputStream fos = new FileOutputStream("bbs.txt");
InputStream is = sock.getInputStream();
byte[] b = new byte[255];
int byteread = 0;
while ((byteread = is.read(b)) != -1) {
fos.write(b, 0, byteread);
}
fos.flush();
fos.close();
sock.close();
}
}
客户端都会了,服务器端怎么就写错了呢????
//: RockServer.java
import java.net.*;
import java.io.*;public class RockServer {
public static final int PORT = 8080;
public static void main(String[] args) throws IOException {
ServerSocket srvSocket = new ServerSocket(PORT);
System.out.println("Start " + srvSocket);
try {
Socket socket = srvSocket.accept();
try {
System.out.println("Connection accepted:" + socket);
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream("RockClient.java")));
PrintWriter out =new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
while(true) {
String str = in.readLine();
if(str==null)
break;
System.out.println("Sending to Client: " + str);
out.println(str);
}
} finally {
System.out.println("Closing...");
socket.close();
}
} finally {
srvSocket.close();
}
}
}//客户端:
////: RockClient.java
import java.net.*;
import java.io.*;public class RockClient {
public static void main(String[] args) throws IOException {
int port = RockServer.PORT;
InetAddress addr =InetAddress.getByName(null);
System.out.println("addr= " + addr);
Socket socket =new Socket(addr, port);
try {
System.out.println("socket= " + socket);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("client.txt"))), true);
while(true) {
String str = in.readLine();
if(str==null)
break;
System.out.println("Receiving from Server: " + str);
out.println(str);
}
} finally {
socket.close();
}
}
}