I'm trying to create a sever application on PC for many android devices using the same wi-fi network.
The devices will find the server's IP by receiving UDP broadcast from it contains the server IP data.
I've started by creating a sample udp broadcaster in C# and udp receiver in java but I never managed to get the packet on the android side . here is the code :
C#:
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Broadcast, listenPort);
listener.Connect(groupEP);
listener.EnableBroadcast = true;
byte[] data = new byte[1024];
try
{
while (!done)
{
Console.WriteLine("broadcast");
Thread.Sleep(400);
listener.Send(data,2);
}
Android code :
DatagramSocket socket;
try {
socket = new DatagramSocket(11000);
socket.connect(getBroadcastAddress(), 11000);
socket.setBroadcast(true);
byte[] buf = new byte[4];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
The Internet Permission is set correctly in the manifest. still not able to receive the packets.
Suggestions:
Make sure you don't have any firewalls (software or hardware) blocking you
Consider using Wireshark:
http://www.wireshark.org/
Look at this example:
http://code.google.com/p/boxeeremote/wiki/AndroidUDP
Related
I have a C# game in unity which has to read from certain port:
Public const string ip = "127.0.0.1";
Public const int port = 5000;
IPEndPoint ipep = new IPEndPoint(IPAdress.parse(ip),port);
UdpClient uc = new UdpClient(ipep);
byte[] res = uc.Recieve(ref ipep);
However I get an error:
A connection attempt failed because the connected party did not respond
while data is being sent on that port from another running app all the time.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Listening to Port 5060
I am developing a SIP client.And I have a question.
I want listen 5060 port for catch the SIP Server Message.For this,I coding something.(Also I take admin rights in program)
But I get SocketException: "An attempt was made to access a socket in a way forbidden by its access permissions" (Native error code: 10013)...
My Code:
private void ListenPort() {
WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
TcpListener server = null;
Int32 port = 5060;
IPAddress localAddr = IPAddress.Parse("192.168.1.33");
server = new TcpListener(localAddr, port);
Byte[] bytes = new Byte[1000];
String data = null;
while (hasAdministrativeRight == true)
{
server.Start();
int i = 0;
while (1==1)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
data = null;
i = stream.Read(bytes, 0, bytes.Length);
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
label3.Text += data;
this.Refresh();
Thread.Sleep(500);
}
}
}
Where do you think the problem?
Have you checked that no other program is already using port 5060? That's what this error can mean.
See this discussion on the matter: http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/d23d471c-002a-4958-829e-eb221c8a4b76/
You need to call server.Start() outside the while loop and before the first AcceptTcpClient call.
Also try using IPAddress.Any instead of IPAddress.Parse("192.168.1.33") for your listener ip
Make sure any other SIP Server program not installed as a Windows service which has using according port.
Type in netstat -an and it will show you the “listening” ports or try to googling port check softwares.
And check your SIP Server configuration for is it running over TCP or UDP.
I am creating a Socket connection with C# client socket and Java Server Socket.
When i am sending data from client socket,the server socket is properly receiving that data.
But when i am trying to send data back to Client socket from Server socket it is getting hanged on client side in receiving data.
Client Side Code(In C#.net)
clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
string hostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(hostName);
System.Net.IPAddress[] ipAddresses = hostEntry.AddressList;
System.Net.IPEndPoint remoteEP =
new System.Net.IPEndPoint(ipAddresses[ipAddresses.Length - 1], port);
clientSocket.Connect(remoteEP);
string sendData = inputFilePath;
byte[] byteDataSend = System.Text.Encoding.ASCII.GetBytes(sendData);
clientSocket.Send(byteDataSend);
int receivedBufferSize = clientSocket.ReceiveBufferSize;
byte[] recivedData = new Byte[receivedBufferSize];
int receivedDataLength = clientSocket.Receive(recivedData);
string stringData = Encoding.ASCII.GetString(recivedData, 0, receivedDataLength);
textFilePath = stringData;
Console.Write(stringData);
clientSocket.Close();
Server Socket Code (In Java)
Socket connection = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
fileName = in.readLine();
convertedFile =runConverter.convertDocumet(fileName);
byte[] sendingData = convertedFile.getBytes("US-ASCII");
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.write(sendingData, 0, sendingData.length);
Tell me what is problem??
Please help...
The usual problem with that kind of c# code is the synchronous receive.
I always recommend doing an asynchronous read, as in this answer.
I'm not positive that that's the source of your problem, but if you implemented the asynchronous receive with a bit of logging there's a good chance that that will either fix your problem or make it much more obvious as to what your problem is.
A hang on synchronous receive does suggest that the Java isn't sending data to the same socket that the c# is listening on, so double-checking those endpoints can also be a good idea.
Hope that helps!
I am writing at NTRIP client on WM6. Basically I am getting data from a server using sockets by first sending a configuration. But I am unable to get it working over a GPRS connection on the same device.
I send this message.
Get / HTTP/1.0
User-Agent: NTRIP client
Accept: */*
Connection: close
To this server.
Hostname: mamba.gps.caltech.edu
Port: 2101
I make the connection by doing this
string message = "GET / HTTP/1.0\r\nUser-Agent: NTRIP client\r\nAccept: */*\r\nConnection: close\r\n\r\n"
IPAddress ipAddress = Dns.GetHostEntry(hostname).AddressList[0];
_NTRIPCaster = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_NTRIPCaster.Connect(new IPEndPoint(ipAddress, Convert.ToInt32(port)));
_NTRIPCaster.Send(Encoding.ASCII.GetBytes(message));
for (int i = 0; i < 50; i++) //Wait for upto 5 seconds for a response
{
Thread.Sleep(100);
if (_NTRIPCaster.Available > 0)
{
Byte[] inBytes = new byte[_NTRIPCaster.Available];
_NTRIPCaster.Receive(inBytes);
sourceTable += Encoding.ASCII.GetString(inBytes, 0, inBytes.Length);
//Check if all of the Source table has been recieved
if (sourceTable.Contains("ENDSOURCETABLE"))
{
sourceTableRecieved = true;
break;
}
}
}
This all works fine if I have a Wi-Fi connection, or the device is docked to a PC and active sync is sharing the PCs internet connection.
If I cut off the internet on the PC, and disable the Wi-Fi then its unable to resolve the hostname to an IP address. Doesn't even get to the socket connections. Basically it is not using the modem in the device and making use of a GPRS connection. This happens whether the GPRS is connected or not.
Since I am on WM6, I have looked at the connection manager API - http://msdn.microsoft.com/en-us/library/aa458120 .
But after following a few other posts I have been able to find on stackoverflow and other forums I have been unable to get it to work. Does anyone know how I can make a GPRS connection and start sending data to a server.
If you use the higher level network objects like the HttpRequest then the connection manager API is automatically invoked by the .NET Framework. Is there a reason you are using low-level sockets?
After alot of experimenting I got it to work.
Used the ConnectionManager, in SDF from OpenNetCF
ConnectionManager connectionManager = new ConnectionManager();
connectionManager.Connect(false);
Thread.Sleep(50); //Give it time to make a connection
Next, I used the TCP/IP method of connection. Honestly, I am not sure how this differs to sockets with a TCP protocol, as from what I can tell a TCPClient object, has a property called Client which itself is a socket. Stripped down sample of code below.
using (NetworkStream ns = _client.GetStream())
using (MemoryStream ms = new MemoryStream())
{
ns.Write(messageBytes, 0, messageBytes.Length);
for (int i = 0; i < 50; i++)
{
Thread.Sleep(20);
byte[] buffer = new byte[16 * 1024];
int bytes;
while ((bytes = ns.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytes);
}
byte[] data = ms.ToArray();
response += Encoding.ASCII.GetString(data, 0, data.Length);
}
I am now getting data sent and received as expected.
How can i get a file from remote computer? i know remote computer ip and 51124 port is open. i need this algorith:(this is a Windows Application visual studio 2008)
1) Connect 192.xxx.x.xxx ip via 51124 port
2) filename:123456 (i want to search it on remote machine)
3) Get File
4) Save C:\
51124 port is open. can i access and can i search any file according to filename?
My code is below:
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 51124);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
sock.Listen(maxConnections);
Socket serverSocket = sock.Accept();
byte[] data = new byte[bufferSize];
int received = serverSocket.Receive(data);
int filenameLength = BitConverter.ToInt32(data, 0);
string filename = Encoding.ASCII.GetString(data, 4, filenameLength);
BinaryWriter bWrite = new BinaryWriter(File.Open(outPath + filename, FileMode.Create));
bWrite.Write(data, filenameLength + 4, received - filenameLength - 4);
int received2 = serverSocket.Receive(data);
while (received2 > 0) {
bWrite.Write(data, 0, received2);
received2 = serverSocket.Receive(data);
}
bWrite.Close();
serverSocket.Close();
sock.Close();
MyQuery(targetip, port, filename) i can use it like that: MyQuery(192.xxx.x.xxx,51124,"MyNeddedFile");
MyQuery(targetip, port, filename)
{
.....
...
..
.
}
You have been trying to ask this question a few times now - perhaps this explains why we cannot answer your question:
If you have an FTP server, it will (by default) listen on port 21. So if I send a message according to the FTP protocol to port 21, it will respond.
If I have apache or IIS (or some other webserver) listening on for instance port 80, and I send an FTP message to it, it will give me an error, because they are expecting HTTP requests.
Without knowing what application is listening on port 51124, we can't possibly tell you how to talk to it.