关于socket连接的问题,进来就有分!!!!!!! 呵呵,就是说ipaddr就是默认你的ip地址啊,会不会别的机端口不行啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我这里也行。楼上的,因该不是端口的问题,如果是端口的问题不是抛出NullPointerException 服务器端package sockettest;import java.io.*;import java.net.*;public class EchoServer { public EchoServer() { } public static void main(String[] args) { try { ServerSocket s = new ServerSocket(80); Socket incoming = s.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter(incoming.getOutputStream(), true); out.println("ok,type 'BYE' to exit"); boolean done = false; while (!done) { String line = in.readLine(); if (line == null) { done = true; } else { out.println("echo:" + line); if (line.trim().equals("BYE")) { done = true; } } } incoming.close(); } catch (Exception e) { e.printStackTrace(); } }} 客户端package sockettest;import java.io.*;import java.net.*;public class EchoClient { public EchoClient() { } public static void main(String[] args){ try{ Socket s = new Socket("172.168.0.14",8189); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter out = new PrintWriter(s.getOutputStream(),true); boolean more = true; while(more){ String line = in.readLine(); if(line == null){ more = false; }else{ System.out.println(line); out.println("BYE"); } } }catch(IOException e){ e.printStackTrace(); } }} 开了服务器,然后用telnet就可以直接测试了。输入"BYE"就退出了。 为什么不直接指定为Localhost呀? String ipaddr=null;Socket socket=new Socket(ipaddr,6633);那台机器的%windir%/system32/drivers/etc下的Host文件有没有给localhost制定IPnew Socket(null,port)参数为NULL不一定就是Localhost,参数为null是指采用环路IP也就是从127.0.0.1开始搜索可用IP 同一个程序点过三到四次的时候就回很慢,使得是ORACLE数据库 tomcat插件不能启动问题 jsp录入中文的问题 JPA缓存的问题,急急急!!!! SpringMVC[面试题] struts 中PO和DAO的问题? structs 在struts-config.xml配置好datasource后,如何使用他的连接池? servlet在tomcat5。0中是不是一定需要先注册? 在Tomcat上部署JAX-RPC的问题 tomcat 4.1.10与sql server2000的数据库连接问题 struts中如何清除保留的值? 请教 tomcat 的简单设置问题。
package sockettest;import java.io.*;
import java.net.*;public class EchoServer {
public EchoServer() {
} public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(80);
Socket incoming = s.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);
out.println("ok,type 'BYE' to exit"); boolean done = false;
while (!done) {
String line = in.readLine();
if (line == null) {
done = true;
} else {
out.println("echo:" + line);
if (line.trim().equals("BYE")) {
done = true;
}
}
}
incoming.close();
} catch (Exception e) {
e.printStackTrace();
}
}}
package sockettest;import java.io.*;
import java.net.*;public class EchoClient {
public EchoClient() {
} public static void main(String[] args){
try{
Socket s = new Socket("172.168.0.14",8189);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
boolean more = true;
while(more){
String line = in.readLine();
if(line == null){
more = false;
}else{
System.out.println(line);
out.println("BYE");
}
}
}catch(IOException e){
e.printStackTrace();
}
}
}
Socket socket=new Socket(ipaddr,6633);那台机器的%windir%/system32/drivers/etc下的Host文件有没有给localhost制定IP
new Socket(null,port)参数为NULL不一定就是Localhost,参数为null是指采用环路IP
也就是从127.0.0.1开始搜索可用IP