gpt4 book ai didi

java - Android应用程序开发中的套接字编程

转载 作者:行者123 更新时间:2023-11-30 04:18:05 25 4
gpt4 key购买 nike

这是我的服务器程序。这很好用。它开始等待客户端。

package jay.com;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class serv {
public static void main(String[] args) throws IOException{
String recvsentence = null;
String sendsentence = null;


ServerSocket server=new ServerSocket(5000);

System.out.println("waiting for client");

Socket sock = server.accept();
System.out.println("Client Connected with IP Address");

DataInputStream dis = new DataInputStream(sock.getInputStream());

DataOutputStream dos = new DataOutputStream(sock.getOutputStream());

recvsentence = dis.readUTF();

System.out.println("Got Data");

sendsentence = recvsentence+"changed";

System.out.println("Message Altered");

dos.writeUTF(sendsentence);

sock.close();

server.close();

}

这是我的客户端程序。套接字未在此处创建....

package jay.com;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Mypro1 extends Activity {
EditText myText,finaltext;
Button button;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myText=(EditText)findViewById(R.id.editText1);
button=(Button)findViewById(R.id.button1);
finaltext=(EditText)findViewById(R.id.editText2);


button.setOnClickListener(new View.OnClickListener(){

public void onClick(View arg0) {
byte adr[] = new byte[4];
adr[0]=(byte)(192);
adr[1]=(byte)(168);
adr[2]=(byte)(1);
adr[3]=(byte)(141);
String sendsentence=null;
String recvsentence=null;
InetAddress addr=null;

try {
addr = InetAddress.getByName("10.0.2.2");
//finaltext.setText(addr.toString());
} catch (UnknownHostException e1) {
e1.printStackTrace();
}

try {
//finaltext.setText("hi");
Socket con=new Socket(addr,5000);

finaltext.setText("Socket Created");

DataInputStream dis = new DataInputStream(con.getInputStream());
DataOutputStream dos = new DataOutputStream(con.getOutputStream());

sendsentence=myText.getText().toString();

dos.writeUTF(sendsentence);

System.out.println("waiting for server response");

recvsentence = dis.readUTF();

System.out.println("got response");

finaltext.setText(recvsentence);

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("Problem while creating");
e.printStackTrace();
}

}
});
}

谁能告诉我问题出在哪里,而且我想知道如何运行该程序。我应该给什么IP地址。我是要尝试连接到 10.0.2.2 还是 192.168.1.141(我的 IP 地址)...

最佳答案

可以通过调用server.getInetAddress();

获取服务器的IP地址

因此,如果您在创建 ServerSocket 后更改行,您将看到服务器的当前 IP 地址。

System.out.println("waiting for client on IP " + server.getInetAddress());

关于java - Android应用程序开发中的套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9679463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com