Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为远程IP建立套接字连接?(我知道如何为本地IP执行此操作)_Java - Fatal编程技术网

Java 如何为远程IP建立套接字连接?(我知道如何为本地IP执行此操作)

Java 如何为远程IP建立套接字连接?(我知道如何为本地IP执行此操作),java,Java,如何为远程IP建立套接字连接?我知道如何使用本地IP实现这一点 我的本地IP代码是: package com.asmaitha.client; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.TextArea; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.DataInputStre

如何为远程IP建立套接字连接?我知道如何使用本地IP实现这一点

我的本地IP代码是:

package com.asmaitha.client;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.logging.Handler;

import javax.print.attribute.standard.Finishings;
import javax.swing.SwingWorker;

public class Client {

    static Socket s,socket;
    DataInputStream dataInputStream;
    DataOutputStream datOutputStream;
    static TextArea tarea;
    String dataFromServer;
    /**
     * @param args
     */
   class MyClient1 extends SwingWorker<String,String>
   {

        @Override
        protected String doInBackground() throws Exception {
            // TODO Auto-generated method stub
            try 
            {
//              InetAddress serverAddress = InetAddress.getByAddress("110.234.149.86",null);//getByName("110.234.149.86") ;
//              tarea.setText(serverAddress.getCanonicalHostName());
                System.out.println("calling socket");
                socket = new Socket("192.168.1.31",8080);
                if(socket != null)
                {
                    System.out.println("ContentApp"+ "Socket Successfully created");
                }
            } 
            catch (IOException e) {
                System.out.println("ContentApp"+ "Socket IOException");
                e.printStackTrace();
            }
            try 
            {
                dataInputStream = new DataInputStream(socket.getInputStream());
                System.out.println("ContentApp"+ "DataInputstream Successfully created");
            }
            catch (IOException e) {
                System.out.println("ContentApp"+ "Datainputstream failed");
                e.printStackTrace();
//              return false;
            }
            try
            {
                datOutputStream = new DataOutputStream(socket.getOutputStream());
                datOutputStream.writeUTF("Hi This is client!");
                System.out.println("ContentApp"+ "Dataoutputstream Successfully created");
            } 
            catch (IOException e) 
            {
                System.out.println("ContentApp"+ "Dataoutputstream failed");
                e.printStackTrace();
//              return false;
            }

            if(socket != null)
            {
               while(socket!=null)
               {
                dataFromServer = dataInputStream.readUTF();
                done();
               }
            }
            return dataFromServer; 
            }
        @Override
        protected void done() {
        if (dataFromServer != null)
        {
            tarea.setText("");
        tarea.append(dataFromServer);
        }
        else
        tarea.append("no value from server !");
        }

        }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("calling theframe");
        Frame frame=new Frame("Button Frame");
        tarea = new TextArea("",5,40);
        frame.add(tarea);
        frame.setLayout(new FlowLayout());
        frame.setSize(300,200);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        System.out.println("calling the Client");
        Client c = new Client();
        MyClient1 myClient = c.new MyClient1() ;

        myClient.execute();

        /*Client1 client = new Client1();
        if(client == null)
        {
            System.out.println("client isnull");
            return;
        }
        Thread clientThread = new Thread(client);
        if(clientThread!=null)
        {
            System.out.println("strating clent1");
            clientThread.start();
        }*/



        /*try {
            s = new Socket("110.234.149.86",8080);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/

    }

}
package com.asmaitha.client;
导入java.awt.FlowLayout;
导入java.awt.Frame;
导入java.awt.TextArea;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入java.io.DataInputStream;
导入java.io.DataOutputStream;
导入java.io.IOException;
导入java.net.InetAddress;
导入java.net.Socket;
导入java.util.logging.Handler;
导入javax.print.attribute.standard.Finishings;
导入javax.swing.SwingWorker;
公共类客户端{
静态插座s、插座;
DataInputStream DataInputStream;
数据输出流数据输出流;
静态区跗骨;
来自服务器的字符串数据;
/**
*@param args
*/
类MyClient1扩展了SwingWorker
{
@凌驾
受保护的字符串doInBackground()引发异常{
//TODO自动生成的方法存根
尝试
{
//InetAddress serverAddress=InetAddress.getByAddress(“110.234.149.86”,null);//getByName(“110.234.149.86”);
//setText(serverAddress.getCanonicalHostName());
System.out.println(“调用套接字”);
插座=新插座(“192.168.1.31”,8080);
if(套接字!=null)
{
System.out.println(“ContentApp”+“已成功创建套接字”);
}
} 
捕获(IOE异常){
System.out.println(“ContentApp”+“Socket IOException”);
e、 printStackTrace();
}
尝试
{
dataInputStream=新的dataInputStream(socket.getInputStream());
System.out.println(“ContentApp”+“DataInputstream已成功创建”);
}
捕获(IOE异常){
System.out.println(“ContentApp”+“Datainputstream失败”);
e、 printStackTrace();
//返回false;
}
尝试
{
datOutputStream=新的DataOutputStream(socket.getOutputStream());
datOutputStream.writeUTF(“嗨,这是客户端!”);
System.out.println(“ContentApp”+“Dataoutputstream已成功创建”);
} 
捕获(IOE异常)
{
System.out.println(“ContentApp”+“Dataoutputstream失败”);
e、 printStackTrace();
//返回false;
}
if(套接字!=null)
{
while(套接字!=null)
{
dataFromServer=dataInputStream.readUTF();
完成();
}
}
从服务器返回数据;
}
@凌驾
受保护的void done(){
if(dataFromServer!=null)
{
跗骨长宽(“”);
附加(dataFromServer);
}
其他的
append(“没有来自服务器的值!”);
}
}
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
System.out.println(“调用帧”);
框架=新框架(“按钮框架”);
tarea=新文本区域(“,5,40);
框架。添加(跗骨);
frame.setLayout(新的FlowLayout());
框架设置尺寸(300200);
frame.setVisible(true);
frame.addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
});
System.out.println(“调用客户端”);
客户机c=新客户机();
MyClient1 myClient=c.new MyClient1();
myClient.execute();
/*Client1 client=newclient1();
if(客户端==null)
{
System.out.println(“客户端为空”);
回来
}
线程clientThread=新线程(客户端);
if(clientThread!=null)
{
System.out.println(“分层元素1”);
clientThread.start();
}*/
/*试一试{
s=新插座(“110.234.149.86”,8080);
}捕获(未知后异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}*/
}
}

您正在连接一个IP地址
新套接字(“192.168.1.31”,8080)您所要做的就是更改地址或给它一个可解析的主机名

我想这不适合你。当你尝试这样做时,你的实际问题是什么


我假设您在防火墙后面的本地网络上。您的网络是否设置为访问您要连接的远程地址?

正如您所建议的,我尝试使用这个新套接字(“Myremoteseveraddress”,8080);但是套接字超时即将到来很可能是网络上的计算机无法连接到该服务器。在它能够做到这一点之前,即防火墙和网络的设置允许这样做,在Java中您无法解决这一问题。在命令行上尝试telnet Myremoteseveraddress 8080
。这必须首先起作用。您必须确定无法连接到此主机的原因并修复它。如果您有一个未插入的鼠标,Java中的任何东西都不允许您使用该鼠标你能解析主机名吗?你能ping主机吗?你能从那台机器ping另一台主机吗?是的,我能ping本地主机,但对于远程IP,我不能建议任何解决方案或代码