我从书上看来的代码,
先看代码:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
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,懒得用那么新,节省资源):
编译并运行:报错,错误一模一样 ...
-------------------------------------------
初学,真心解决不了,求教!
------解决方案--------------------
你的客户端的代码呢?
------解决方案--------------------
你的服务器端代码呢?刚才说错了,不好意思
------解决方案--------------------
没有server当然连不上。
------解决方案--------------------
socket都是成对出现的,而且启动顺序是先启动服务端代码(ServerSocket说:我在这儿等着你到来),然后启动客户端代码(Socket:留下来~)。
仔细看看书,应该还有个ServerSocket的实例的。
------解决方案--------------------
嗯,目测你本机ip是192.168.6.196。
因此应该编写另外一个服务端程序,监听本机ip,然后先运行那个程序,再运行本程序。
------解决方案--------------------
不知道服务,连不上服务,看一下端口是否有问题,
------解决方案--------------------
自己改下对应的端口号
猜测应该是80,常用WEB端口号
String[] args = { "192.168.6.196", "i love you ", "80" };
自己配置的端口号推荐3000以后的端口,比较可靠不容易占用
具体查阅的话,可以找服务那边的
ServerSocket serverSocket = new ServerSocket(5000);
这样一句,括号里面的代表服务器的端口号
------解决方案--------------------
再补一句,如果是端口占用了不是报那个错误,是这个:
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)
------解决方案--------------------