程序冻结在Java中的套接字编程

时间:2018-03-12 11:17:58

标签: java sockets network-programming serversocket

我制作了这个程序服务器和客户端但程序冻结。我从服务器程序中删除了DataInputStream。然后它工作正常

服务器程序:

import java.net.*;
import java.io.*;
import java.util.*;

class Server2
{
public static void main(String args[])throws Exception
{
    ServerSocket ss=new ServerSocket(9900);
    System.out.println("Waiting For Client to Connect");
    Socket s=ss.accept();
    System.out.println("connection established");
    Scanner sc=new Scanner(System.in);
    System.out.println("enter string:");
    String p=sc.nextLine();
    DataOutputStream d=new DataOutputStream(s.getOutputStream());
    d.writeBytes(p);

    System.out.println("String Swaped character!!!");
    DataInputStream in=new DataInputStream(s.getInputStream());
    System.out.println("Break");
    System.out.println(in.readUTF());
    d.close();
    in.close();

}
}

客户计划:

import java.io.*;
import java.net.*;
import java.util.*;

class Client2
{
public static void main(String arg[])throws Exception
{
    Socket s=new Socket("LocalHost",9900);
    DataInputStream d2=new DataInputStream(s.getInputStream());

    String r=d2.readLine();
    DataOutputStream out= new DataOutputStream(s.getOutputStream());
    out.writeUTF(r);

    d2.close();
    System.out.println(r);


   }
}

我做了许多类似的节目但是没有遇到这样的问题。任何人都可以帮我解决什么错误。谢谢你提前:)

0 个答案:

没有答案