日期:2014-05-20 浏览次数:20833 次
下面代码哪错了,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();
}
}