请问各位前辈,这连个使用了套接字的程序为何会连接不上呢?代码完全没有错误啊!请大家指点一下!
import java.io.*;
import java.net.*;
public class TestServer {
public static void main(String args[])
{
ServerSocket server = null;
Socket socket;
String s=null;
DataOutputStream out =null;
DataInputStream in = null;
try{
server = new ServerSocket(4141);
}
catch(IOException ee){
System.out.println("没有连上"+ee);
}
try{
System.out.println("waiting the client...");
socket = server.accept();
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
while(true){
s = in.readUTF();
int m = Integer.parseInt(s);
out.writeUTF("hello,i am the server");
out.writeUTF("the num*2  is:"+m*2);
System.out.println("server receive:"+m);
Thread.sleep(500);
}
}
catch(Exception e){
System.out.println("lose conection!");
}
}}
-----------------------------------------
Client:
import java.io.*;
import java.net.*;
public class TestClient {
public static void main(String args[])
{
 String s=null;
 Socket socket;
 DataInputStream in = null;
 DataOutputStream out = null;
 try{
 socket = new Socket("127.0.0.1",4141);
 in = new DataInputStream(socket.getInputStream());
 out = new DataOutputStream(socket.getOutputStream());
 for(int i =1 ; i<100; i=i+2){
 out.writeUTF("   "+i);
 s=in.readUTF();
 System.out.println("client have received:"+s);
 Thread.sleep(500);
 }
 }
 catch(Exception e){
 System.out.println("client lose conection!");
 }
}}

解决方案 »

  1.   

    你用ipconfig查一下自己机器的IP地址,用得到地址,换掉socket = new Socket("127.0.0.1",4141);语句中的,127.0.0.1。
     
      

  2.   

    你是局域网上调试,还是单机internet上调试?
      

  3.   

    服务器端改下可以发送数据到客户端其他自己漫漫来吧楼主
    public class TestServer { 
    public static void main(String args[]) 

    ServerSocket server = null; 
    Socket socket; 
    String s=null; 
    DataOutputStream out =null; 
    DataInputStream in = null; 
    try{ 
    server = new ServerSocket(4141); 

    catch(IOException ee){ 
    System.out.println("没有连上"+ee); 

    try{ 
    System.out.println("waiting the client..."); 
    socket = server.accept(); 
    out = new DataOutputStream(socket.getOutputStream()); 
    //in = new DataInputStream(socket.getInputStream()); 
    while(true){ 
    //s = in.readUTF(); 
    //int m = Integer.parseInt(s); 
    //发送数据
    out.writeUTF("hello,i am the server"); 
    //out.writeUTF("the num*2  is:"+m*2); 
    //System.out.println("server receive:"+m); 
    //Thread不是这样写的  具体逻辑发送和接收分开 网上搜很多
    //Thread.sleep(500); 


    catch(Exception e){ 
    System.out.println("lose conection!"); 

    }  } 
      

  4.   


    你用ipconfig查一下自己机器的IP地址,用得到地址,换掉socket = new Socket("127.0.0.1",4141);语句中的,127.0.0.1。那你就用这个办法试试吧。。
      

  5.   

    int m = Integer.parseInt(s);  <<-------------------原来是这句出错,呵呵  以前是可以运行的,奇怪了!!
      

  6.   

    int m = Integer.parseInt(s);  
    先保证s!=null就行了
      

  7.   

    不是连接不上,是服务器端抛出了异常而已。
    Server中:
    int m = Integer.parseInt(s);   该句抛出了解析格式错误的异常,导致服务器退出,从而客户端失去了连接。原因:客户端向服务器端发送的数据是" " + i,里面含有一个空格,这种字符串不能被解析成int形式,请把字符串里边的空格去掉。建议:
       以后编写异常处理的时候尽量把异常内容打印出来,便于查找错误。
      

  8.   

    遇到过类似的问题, 服务端只要报异常有事tomcat时会自动停止的,我有次就是有个线程没关闭,每次tomcat都自动就关了,后来调试了好久才搞定的. 看来以后要细心啊!
      

  9.   

    真不知道是哪位计算机天才,硬把socket翻译成套接字,搞得一点都不理解.