下面代码哪错了,socket就是连不起来:
import java.io.*;
import java.net.*;
class TransClient{
public static void main(String[] args) throws Exception{
InetAddress ia = InetAddress.getLocalHost();
Socket s = new Socket(ia.getHostAddress(), 10005);
//源:键盘
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
BufferedReader bufIn =
new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bufOut =
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line = null;
while((line = bufr.readLine()) != null){
if("over".equals(line)){
break;
}
bufOut.write(line);
bufOut.newLine();
bufOut.flush();
String str = bufIn.readLine();
System.out.println("Server: " + str);
}
bufr.close();
s.close();
}
}
public class TransServer {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println("IP: " + ip + "...connected...");
BufferedReader bufIn =
new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bufOut =
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line = null;
while((line = bufIn.readLine()) != null){
//从客户端读到文本,然后转成大写形式反馈给客户端.
bufOut.write(line.toUpperCase());
bufOut.newLine();
bufOut.flush();
}
s.close();
ss.close();
}
}
------解决方案--------------------
楼主怎么运行的?
代码貌似没问题。服务端需要保持运行,再运行客户端,发送数据到服务端。
然后应该可以接到的。
------解决方案--------------------