如何为远程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();
}*/
}
}
答案 0 :(得分:0)
您正在连接到IP地址new Socket("192.168.1.31",8080);
您只需更改地址或为其提供可解析的主机名。
我认为这不适合你。当你尝试这样做时,你的实际问题是什么?
我假设你在防火墙后面的本地网络上。您的网络是否设置为访问要连接的远程地址?