簡體   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