Android and PC socket (TCP/IP) Communication (by USB) Refuse to connect

Source: Internet
Author: User
Android and PC socket (TCP/IP) Communication (by USB) Refuse to connect Time:
Source:
Csdn community Author:
Joy_liut Click:


Scenario 1:
Android_server:
Final int server_port = 12345;
Private void startserver ()
{
Try
{
Log. E ("s: Connecting ...","");
Serversocket = new serversocket (server_port );
While (true)
{
Socket Client = serversocket. Accept ();
Log. E ("s: inserting ing...", "jjjjjj ");
Try
{
Bufferedreader in = new bufferedreader (New inputstreamreader (client. getinputstream ()));
String STR = in. Readline ();
Log. E ("s: Received: '", "%" + Str );
}
Catch (exception E)
{
Log. E ("s: error ","");
E. printstacktrace ();
}
Finally
{
Client. Close ();
Log. E ("s: Done ","");
}
Thread. Sleep (3000 );
}
}
Catch (exception E)
{
Log. E ("s: error ","");
E. printstacktrace ();
}
}

Pc_client:
Try
{
Runtime.getruntime(cmd.exe C ("ADB forward TCP: 12581 TCP: 12345 ");
}
Catch (ioexception E3)
{
E3.printstacktrace ();
}

Socket socket = NULL;
Try
{
Inetaddress serveraddr = NULL;
Serveraddr = inetaddress. getbyname ("127.0.0.1 ");
System. Out. println ("TCP 1111" + "C: Connecting ...");

Socket = new socket (serveraddr, 12581); // here, port 12581 is open to the PC and has been redirected to port 12345 of the device.

String message = "androidres, where is my pig (Android )? ";
System. Out. println ("TCP 2222" + "C: sending: '" + message + "'");
Printwriter out = new printwriter (New bufferedwriter (New outputstreamwriter (socket. getoutputstream ())),

True );
Out. println (Message );
}
Catch (unknownhostexception E1)
{
System. Out. println ("TCP 3333" + "error:" + e1.tostring ());
}
Catch (ioexception E2)
{
System. Out. println ("TCP 4444" + "error:" + e2.tostring ());
}
Finally
{
Try
{
If (socket! = NULL)
{
Socket. Close ();
}
}
Catch (ioexception E)
{
System. Out. println ("TCP 5555" + "error:" + E. tostring ());
}
}
}
Android_server and pc_client are connected successfully ~

Scenario 2:
Pc_server:
Try
{
Runtime.getruntime(cmd.exe C ("ADB forward TCP: 12555 TCP: 12345 ");

System. Out. println ("s: Connecting ...");
Serversocket = new serversocket (server_port );
While (true)
{
Socket Client = serversocket. Accept ();
System. Out. println ("s: Starting ing ...");
Try
{
Bufferedreader in = new bufferedreader (New inputstreamreader (client. getinputstream ()));
String STR = in. Readline ();
System. Out. println ("s: pinned ed: '" + STR + "'");
}
Catch (exception E)
{
System. Out. println ("s: error ");
E. printstacktrace ();
}
Finally
{
Client. Close ();
System. Out. println ("s: Done ");
}
}
}
Catch (exception E)
{
System. Out. println ("s: error ");
E. printstacktrace ();
}
Android_client:
Socket socket = NULL;
Try
{
Inetaddress serveraddr = NULL;
Serveraddr = inetaddress. getbyname ("127.0.0.1 ");
Log. E ("TCP 1111", "C: Connecting ...");

Socket = new socket (serveraddr, 12345 );

String message = "androidres, where is my pig (Android )? ";
Log. E ("TCP 2222", "C: sending: '" + message + "'");
Printwriter out = new printwriter (New bufferedwriter (New outputstreamwriter (socket. getoutputstream ())),

True );
Out. println (Message );
}
Catch (unknownhostexception E1)
{
Log. E ("TCP 3333", "error:" + e1.tostring ());
}
Catch (ioexception E2)
{
Log. E ("TCP 4444", "error:" + e2.tostring ());
}
Finally
{
Try
{
If (socket! = NULL)
{
Socket. Close ();
}
}
Catch (ioexception E)
{
Log. E ("TCP 5555", "error:" + E. tostring ());
}
}
An error occurred while connecting android_client to pc_server ~ Throw the following exception:
E/tcp 1111 (12986): C: Connecting...
E/tcp 4444 (12986): Error: java.net. connectexception:/127.0.0.1: 12345-connection refused

My problem is:
1. When Android communicates with a PC, can it be used as a server only on Android?
2. If android can be used as a client, how can it be implemented? What is the problem with the above Code?
3. If you have more related knowledge, please attach it to share it. Thank you ~~

1. To use socket communication, add permission: Android. Permission. Internet to mainfest. xml.
2. If you do not have the root permission, you cannot bind a port lower than 1024.

About Reply 1: Permission is added, otherwise case 1 will not pass;

My question is, why can scenario 1 be successful, and scenario 2 cannot?

Thank you for your answers ~

Change 127.0.0.1 to 10.0.2.2.

Attention.

Top !!!!!!!!!!!

Paste it!
There is a very urgent problem:

What should I do if I use USB to Configure TCP/IP connections?

Thank you!

This post is based on USB connections ~

Finally, I passed. Thank you for your answers!

After the USB cable is plugged in, how can I know the IP address of the PC if the Android mobile phone is a client?

Android and PC communication havePort redirection
Because Android is no longer used, the answer to this question is unclear. Please search for relevant information ;)

If two Android simulators are used for communication, the simulator with the port number 5556 serves as the server, and the simulator with the port number 5554 serves as the client, is the address between them 10.0.2.2? I tried it. Why not? No. I use UDP for transmission.

The simulator can be used to access each other, but it cannot be accessed when you connect to your mobile phone with a USB!

Thank you. I am very helpful in Android socket with my discussion on this issue.

Why can't I see a reply after I log on ..

Could you connect your first server on the simulator and the client on the PC?
Runtime.getruntime(cmd.exe C ("ADB forward TCP: 12581 TCP: 12345 ");
Does this statement fail?

I haven't done this for a long time, but you asked, "Can you connect your first server on the simulator and the client on the PC ?" Yes. At that time, our project was implemented in this way.

... Paying attention

??? I want to know if androidis used in the server by using runtime.getruntime(cmd.exe C ("ADB forward TCP: 12555 TCP: 12345? What is the purpose of this sentence? I hope the landlord can help me answer it. Thank you.

Android can also be used as a sever. Otherwise, let's get rid of Google.

In the second case, no connection is available on the real machine. I don't know if the landlord has tried it on a real machine, but I still need to handle it.

Tag: Android and PC
Socket
Communication
TCP
IP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.