Java 在 Eclipse 中运行套接字编程代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24325928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:34:34  来源:igfitidea点击:

Running Socket Programming code in Eclipse

javasockets

提问by BumbleBee

I have written a program on Socket Programming, and I created a client and a server. Codes for both are as follows:

我写了一个关于 Socket Programming 的程序,我创建了一个客户端和一个服务器。两者的代码如下:

CLIENT:

客户:

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

 public class GreetingClient
 {
    public static void main(String [] args)
    {
      String serverName = args[0];
      int port = Integer.parseInt(args[1]);
      try
      {
         System.out.println("Connecting to " + serverName
                             + " on port " + port);
         Socket client = new Socket(serverName, port);
         System.out.println("Just connected to "
                      + client.getRemoteSocketAddress());
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out =
                       new DataOutputStream(outToServer);

         out.writeUTF("Hello from "
                      + client.getLocalSocketAddress());
         InputStream inFromServer = client.getInputStream();
         DataInputStream in =
                        new DataInputStream(inFromServer);
         System.out.println("Server says " + in.readUTF());
         client.close();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

SERVER:

服务器:

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

 public class GreetingServer extends Thread
 {
    private ServerSocket serverSocket;

    public GreetingServer(int port) throws IOException
    {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(10000);
    }

    public void run()
    {
      while(true)
      {
         try
         {
            System.out.println("Waiting for client on port " +
            serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to "
                  + server.getRemoteSocketAddress());
            DataInputStream in =
                  new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());
            DataOutputStream out =
                 new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to "
              + server.getLocalSocketAddress() + "\nGoodbye!");
            server.close();
         }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
       }
     }
     public static void main(String [] args)
     {
      int port = Integer.parseInt(args[0]);
      try
      {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

Now, I am unable to run the program in Eclipse, can anyone help me, how to do this ?

现在,我无法在 Eclipse 中运行该程序,任何人都可以帮助我,该怎么做?

采纳答案by Am_I_Helpful

Actually, you need to run the programs individually in the Eclipse-IDE. The output will be indented one on another in next tab on the output area.

实际上,您需要在 Eclipse-IDE 中单独运行程序。输出将在输出区域的下一个选项卡中一个接一个缩进。

I don't have info about Eclipse.

我没有关于 Eclipse 的信息。

In Netbeans,you need to run the Client.java file separately. Then,move to Server.java file and run it separately. You'll see at the bottom that two windows---one running Client.java and the other running Server.java will be running independently. Now,send message from client to server and vice-versa.

在 Netbeans 中,您需要单独运行 Client.java 文件。然后,移动到 Server.java 文件并单独运行它。您将在底部看到两个窗口——一个运行 Client.java,另一个运行 Server.java 将独立运行。现在,从客户端向服务器发送消息,反之亦然。

EDIT FOR YOUR COMMAND LINE PARAMETER SETTING IN ECLIPSE IDE :-

在 ECLIPSE IDE 中编辑您的命令行参数设置:-

Go to Project--> Run --> Run Configurations --> Arguments.!

转到项目--> 运行--> 运行配置--> 参数。!

Pass the arguments as

将参数传递为

args[0]=127.0.0.1 //local-host

args[0]=127.0.0.1 //本地主机

args1=3000 //say 3000,you can give any port no. but take care that it should exist!

args 1=3000 //说3000,你可以给任何端口号。但要注意它应该存在!

Parameter passing

参数传递

回答by Naval Kishor Jha

Go to RunConfigurations..and click on the Class Name(as here 'GreetingClient') under the java application in the left pane of RunConfiguration window

转到RunConfigurations..并单击GreetingClientRunConfiguration 窗口左窗格中 java 应用程序下的类名(如此处的“ ”)

on the right side you will get many tab like Main,Arguments,jre,ClassPathetc so now click on the 'Arguments' below this tag you will get textbox with label Program arguments:here in this textbox you need to pass your command line argument

在右边你会得到许多标签一样MainArgumentsjreClassPath等所以现在点击“ Arguments”这个标签,你会得到的文本框与标签下面Program arguments:这个文本框,你需要通过你的命令行参数在这里

for multiple values give single spacebetween the argument valuesthen click on the Applybutton

对于多个值single spaceargument values然后单击Apply按钮

like in above case you need to pass commandline argument twice. so first you configure for the GreetingServerand then for the GreetingClientand then applyand runone by one

就像上面的情况一样,您需要两次传递命令行参数。所以首先您配置GreetingServer,然后为GreetingClient,然后applyrun一个接一个

click on the GreetingServer.javaand then right click on mouse and select Run As-->Run Configuration..then go to Java Applicationand click

单击GreetingServer.java然后右键单击鼠标并选择Run As-->Run Configuration..然后转到Java Application并单击

GreetingServer -->Argument--> 6000   -->apply and -->run

output like this

像这样输出

Waiting for client on port 6000...

now click on the GreetingClient.javaand then right click on mouse and select Run As-->Run Configuration..then go to Java Applicationand click

现在单击GreetingClient.java然后右键单击鼠标并选择Run As-->Run Configuration..然后转到Java Application并单击

GreetingClient -->Argument--> 127.0.0.1 6000   -->apply and -->run

then you will get your application running and output like this

然后你会让你的应用程序运行并像这样输出

Connecting to 127.0.0.1 on port 6000
Just connected to /127.0.0.1:6000
Server says Thank you for connecting to /127.0.0.1:6000
Goodbye!

Any port you can send it your wise just keep in mind port no. should be free for the eclipse argument passing you can go through this link

您可以明智地发送任何端口,只需记住端口号。eclipse参数传递应该是免费的,你可以通过这个链接

回答by sumit s lal

Try running the Client program first and then run the server program in Netbeans, the program will run with no problem...

尝试先运行Client程序,然后在Netbeans中运行server程序,程序运行没有问题...

回答by user9844251

/*Server*/    
import java.net.*;        
import java.io.*;        
public class MyServer 
{
public static void main(String args[])throws Exception
    {
    ServerSocket ss=new ServerSocket(8100);
    Socket s=ss.accept();
    DataInputStream din=new DataInputStream(s.getInputStream());
    DataOutputStream dout=new DataOutputStream(s.getOutputStream());
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String str="",str2="";
    while(!str.equals("stop"))
    {
    str=din.readUTF();
    System.out.println("clint Says"+str);
    str2=br.readLine();
    dout.writeUTF(str2);
    dout.flush();
    }
    din.close();
    s.close();
    ss.close();
    }
}