繁体   English   中英

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

[英]How to make socket connection for remote IP ? (I know how to do it for Local IP)

如何建立远程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();
        }*/

    }

}

您正在连接到IP地址new Socket("192.168.1.31",8080); 您所要做的就是更改地址或为其提供可解析的主机名。

我认为这对您不起作用。 当您尝试这样做时,您的实际问题是什么?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM