public class TCPEchoClient { public static void main(String[] arg) throws UnknownHostException, IOException{ String[] args = { "192.168.6.196", "i love you ", "8" }; if ((args.length < 2) || (args.length > 3)) throw new IllegalArgumentException("Parameter(s): <Server> <Word> [<Port>]"); String server = args[0]; byte[] data = args[1].getBytes(); int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7; Socket socket = new Socket(server, servPort); System.out.println("Connected to server...sending echo string"); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); out.write(data); int totalBytesRcvd = 0; int bytesRcvd; while (totalBytesRcvd < data.length) { if ((bytesRcvd = in.read(data, totalBytesRcvd, data.length - totalBytesRcvd)) == -1) throw new SocketException("Connection closed prematurely"); totalBytesRcvd += bytesRcvd; } System.out.println("Received: " + new String(data)); socket.close(); } }
jdk javac 编译: 编译 : ok [可以cmd看] 运行 :报错,Exception in thread "main" java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.<init>(Unknown Source) at java.net.Socket.<init>(Unknown Source) at TCPEchoClient.main(TCPEchoClient.java:20) 接着,换用MyEclipse(我用的是6.5,懒得用那么新,节省资源): 编译并运行:报错,错误一模一样 ...
java.net.BindException: Address already in use: JVM_Bind at java.net.TwoStacksPlainSocketImpl.socketBind(Native Method) at java.net.TwoStacksPlainSocketImpl.socketBind(Unknown Source) at java.net.AbstractPlainSocketImpl.bind(Unknown Source) at java.net.TwoStacksPlainSocketImpl.bind(Unknown Source) at java.net.PlainSocketImpl.bind(Unknown Source) at java.net.ServerSocket.bind(Unknown Source) [解决办法] 书上应该是有两段代码的,只有先运行服务器上那部分代码,再运行客户端上那部分代码,才能成功的…… [解决办法]
public static void main(String[] args) throws IOException {
if ((args.length < 2) [解决办法] (args.length > 3)) // Test for correct # of args throw new IllegalArgumentException("Parameter(s): <Server> <Word> [<Port>]");
String server = args[0]; // Server name or IP address // Convert argument String to bytes using the default character encoding byte[] data = args[1].getBytes();
int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7;
// Create socket that is connected to server on specified port Socket socket = new Socket(server, servPort); System.out.println("Connected to server...sending echo string");
InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream();
out.write(data); // Send the encoded string to the server
// Receive the same string back from the server int totalBytesRcvd = 0; // Total bytes received so far int bytesRcvd; // Bytes received in last read while (totalBytesRcvd < data.length) { if ((bytesRcvd = in.read(data, totalBytesRcvd, data.length - totalBytesRcvd)) == -1) throw new SocketException("Connection closed prematurely"); totalBytesRcvd += bytesRcvd; } // data array is full
System.out.println("Received: " + new String(data));
socket.close(); // Close the socket and its streams } }