简体   繁体   English

将Socket转换为String,反之亦然

[英]Convert Socket to String and vice versa

I'm socket programming for Android (in Java). 我正在为Android(使用Java)进行套接字编程。 How can I convert my socket to a string and convert that string representation back to a socket? 如何将我的套接字转换为字符串并将该字符串表示形式转换回套接字?

For example, consider a Socket ss . 例如,考虑一个Socket ss What I want is some thing like this: 我想要的是这样的事情:

String strss = ss.tostring();

and later 然后

Socket s = new Socket(strss);

I want to save the Socket (ie the details needed to recreate that socket later) into a database (as a string) and after that I need to reconvert it to Socket to use it in my app! 我想将Socket(即稍后重新创建该套接字所需的详细信息)保存到数据库中(作为字符串),之后我需要将其重新转换为Socket以在我的应用程序中使用它!

Strange question but you can extract the info you need to recreate the socket from a string. 奇怪的问题,但是您可以从字符串中提取重新创建套接字所需的信息。 Without error checking etc: 没有错误检查等:

To string: 字符串:

Socket s = ...;
String hostAndPort = s.getInetAddress().getHostAddress();
hostAndPort = hostAndPort + ":" + s.getPort();

then back: 然后返回:

String host = hostAndPort.substring(0, hostAndPort.indexOf(':'));
int port = Integer.parseInt(hostAndPort.substring(hostAndPort.indexOf(':') + 1));
Socket s = new Socket(host, port);

I want to save Socket into database (as string ) 我想将Socket保存到数据库中(作为字符串)

That is not possible. 这是不可能的。

You are welcome to save the host and port you used to open a socket to a database. 欢迎您保存用于打开数据库套接字的主机和端口。

You can't turn a socket into a string, except as a simple summary. 除了简单的摘要外,您无法将套接字转换为字符串。 Instead, you should send data over the socket. 相反,您应该通过套接字发送数据。

try{
Socket ss=new Socket();
BufferedReader in=new BufferedReader(new InputStreamReader(ss.getInputStream()));
PrintWriter out=PrintWriter(ss.getOutputStream()));
String line=in.readLine(); //reads a line, repeat as necessary
out.println("foo"); //Writes a line
} catch (IOException e){
     e.printStackTrace();
}

Output buffering is left as an exercise to the reader. 输出缓冲留给读者作为练习。

You can't save a socket into a database. 您无法将套接字保存到数据库中。 It doesn't make any sense. 它没有任何意义。

To recreate a socket later, you just need the host address and port number . 要稍后重新创建套接字,您只需要主机地址端口号

  • If each existing socket has a different host address and port number then you will need to store both in your database: 如果每个现有的套接字都有不同的主机地址端口号,则需要将它们都存储在数据库中:

    • If you have no choice about storing all the data in a single string, then you will need to encode both host address and port number in that string, in the way suggested by Mattias or something similar. 如果您无法选择将所有数据存储在单个字符串中,则需要按照Mattias或类似方法建议方式对该字符串中的主机地址和端口号进行编码。

    • It would be better though, to store the host address and the port number in the database separately, that way you do not have the added complication of creating and parsing the strings later. 但是,将主机地址端口号分别存储在数据库中会更好,这样您就不会在以后创建和解析字符串时增加复杂性。

  • If your socket is always opened to the same port however, you could just save the host address in the database. 但是,如果套接字始终打开到同一端口,则只需将主机地址保存在数据库中。

For example, to get the host address : 例如,要获取主机地址

String strss = ss.getInetAddress().getHostAddress();

then to recreate the socket: 然后重新创建套接字:

Socket s = new Socket(strss, KNOWN_PORT);

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

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