I am able to connect to TCP IP netwrok but even if i am sending some
byte array but yet i am not seeing any output on Server console.
Server is C++ and i need to send data from my android apps. I am able
to connect but when i send data i am not getting any notification. the
followins is my code.
InetAddress serverAddr = InetAddress.getByName(serverIP);
SocketAddress socketadd= new InetSocketAddress(serverAddr, serverPort);
Log.d("TCP", "C: Connecting...");
//Socket socket = new Socket(serverAddr, serverPort);
Socket socket=new Socket();
try {
Log.d("TCP", "C: Sending: '" + msg + "'");
socket.connect(socketadd);
Log.e("Connect:", "Connect:");
DataOutputStream dataout = new
DataOutputStream(socket.getOutputStream());
dataout.flush();
byte haeader[]=new byte[6];
// String data="20110110,cswxerotest,cswxerotest";
// Packet p=new Packet();
//byte bdata[]=converttoCPP(getBytes());
byte bdata[]=getBytes();
//byte a[]=new byte[20];
//dataout.writeByte(5);
dataout.write(bdata,0,bdata.length);
dataout.flush();
I see you have a message msg to send, but I don't see that you convert it to bytes and send it anywhere (something like byte bdata[] = msg.getBytes() )
Related
I am developing an Android multiplayer game using Wi-Fi direct.
Using Wi-Fi direct, I am able to establish connection between peer devices. Also the code is able to send the data from client device to server device. (According to Android documentation, the Wi-Fi direct uses client-server model)
Problem:
I am not able to share the data from server device to client device using Wi-Fi direct.
I have following questions:
Is there any other way to transfer data (bi-directional) between two
Android devices which are connected through Wi-Fi Direct?
During my online research I understood that to send data from server
device to client device, server needs to know the client’s IP
address. How to use this client’s IP address to send data from
server device to client device? (I am able to fetch the client’s IP
address)
I'd appreciate any suggestions on these queries. Thank you in advance.
Code:
Server Side
public class DataTransferAsyncTask extends AsyncTask<Void,Void,String>{
ServerSocket serverSocket;
Socket client;
InputStream inputstream;
Context context = mActivity.getApplicationContext();
String s;
InetAddress client_add;
#Override
protected String doInBackground(Void... voids) {
try{
serverSocket = new ServerSocket(9999);
Log.e("hello", "Server: Socket opened");
client = serverSocket.accept();
Log.e("hello", "Server: connection done");
inputstream = client.getInputStream();
// OutputStream outputStream = serverSocket.getO
//getting data from client
byte[] address = new byte[12];
if(client.isConnected())
inputstream.read(address);
s = new String(address);
String only_add = new String();
only_add = s.substring(0,12);
client_add = InetAddress.getByName(only_add);
Log.e("hello", "Server: clients ip 1 " + only_add);
Log.e("hello", "Server: converted address 1 " + client_add + " \n is connected"+
client.isConnected());
//send data to client
OutputStream stream = client.getOutputStream();
stream.write(s.getBytes());
Log.e("hello","context value "+context);
// cancel(true);
}catch (IOException e){
}
return null;
}
}
Client Side:
#override
protected void onHandleIntent(Intent intent) {
Log.e("hello","client socket");
Toast.makeText(this,"client socket",Toast.LENGTH_LONG).show();
Context context = getApplicationContext();
if(intent.getAction().equals(action_send_data)){
String host = intent.getStringExtra(group_owner_address);
Socket socket = new Socket();
int port = intent.getIntExtra(group_owner_port,9999);
//binding connection
try{
String x="hello";
Log.e("hello","opening client socket");
byte[] address = getLocalAddress();
String ipAdd = getDottedDecimalIP(address);
socket.bind(null);
socket.connect(new InetSocketAddress(host,port),socket_timeout);
Log.e("hello","device socket address "+ socket.getInetAddress() );
Log.e("hello","client socket is connected"+socket.isConnected());
Log.e("hello","device address :"+ipAdd + " byte "+ address);
//sending data to server
OutputStream stream = socket.getOutputStream();
stream.write(ipAdd.getBytes());
//getting data from the server(supposed to)
InputStream inputstream = socket.getInputStream();
byte[] address_to_sto_fr_ser = new byte[15] ;
inputstream.read(address_to_sto_fr_ser);
String s = new String(address_to_sto_fr_ser);
Log.e("msg from server","msg from server "+s);
// stream.close();
// is.close();
}catch (IOException e){
}
}
}
The communication between client and WiFi Direct Group Owner is based on Socket server running on the Group Owner and clients connected to that server.
Once WiFi Direct group is formed(you can know that by checking "onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo)"), check if the current device is the Group Owner and if so, start the sockets server (similar to your code):
mServerSocket = new ServerSocket(mPort);
Log.e(getClass().getSimpleName(), "Running on port: " + mServerSocket.getLocalPort());
Then next, add this line to accept connections and store a reference of the client socket:
Socket mSocket = mServerSocket.accept();
Now, you have a reference of the client socket, you can use it to send data / messages to it. Next, the other device (client) should initiate a connection to the socket server:
mSocket = new Socket();
mSocket.bind(null);
mSocket.connect(new InetSocketAddress(mAddress, mPort), 500);
To send message from server to client:
DataOutputStream mDataOutputStream = new DataOutputStream(mSocket.getOutputStream());
Send simple message:
mDataOutputStream.writeUTF(message);
mDataOutputStream.flush();
Hope this helps.
I saw many pages, and try many advices, but still can't receive data via UDP on device (nexus 4). I work in my LAN, over wifi (3G is off).
I have a client, on PC (192.168.1.5) and consumer on Android device (192.168.1.3:54445)
Here is a client code:
String string = "hello udp";
DatagramSocket socket = null;
try {
address = InetAddress.getByName("192.168.1.3");
socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 54445);
socket.send(packet);
Here is a consumer code:
DatagramSocket socket = null;
stop = false;
InetAddress address = null;
try {
socket = new DatagramSocket(54445);
while (!stop) {
byte[] buffer = new byte[minBufSize];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
Log.d(this.getClass().getSimpleName(), String.valueOf(packet.getLength()));
Debugger stop on socket.receive(packet). Consumer work in AsyncTask.
Thank you!
It's my fault, at first check you router settings.
I have two Android Devices, one with IP address 10.0.0.6 and another 10.0.0.9 I can ping both of them on my computer.
I send out a broadcast message from 10.0.0.6 UDP package and verified that with my wireshark,
the broadcast address is 10.0.0.255
DatagramSocket socket = new DatagramSocket(3400);
socket.setBroadcast(true);
byte[] buf = ("Hello from Android").getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, getBroadcastAddress(), 3400);
socket.send(packet);
However, my other android device didn't receive anything from that broadcast message.
DatagramSocket socket = new DatagramSocket(3400);
byte[] buf = new byte[30];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
It never come back from socket.receive.
I'm trying to keep a listening UDP port on 23000, the app works well in local environment.
But switching to 3G there are no chances, traffic is blocked.
I tried 'opening' the carrier channel sending 4bytes of data on the same port before receiving:
InetAddress serverAddr = InetAddress.getByName(SOULISSIP);
DatagramChannel channel = DatagramChannel.open();
socket = channel.socket();
//socket = new DatagramSocket();
socket.setReuseAddress(true);
//InetSocketAddress ia = new InetSocketAddress("localhost", SERVERPORT);
InetSocketAddress sa = new InetSocketAddress(SERVERPORT);
socket.bind(sa);
DatagramPacket holepunh = new DatagramPacket(new byte[]{0,1,2,3},4, serverAddr, SERVERPORT);
socket.send(holepunh);
// create a buffer to copy packet contents into
byte[] buf = new byte[200];
// create a packet to receive
DatagramPacket packet = new DatagramPacket(buf, buf.length);
Log.d("UDP", "***Waiting on packet!");
socket.setSoTimeout((int) opzioni.getDataServiceInterval());
// wait to receive the packet
socket.receive(packet);
UDPSoulissDecoder.decodevNet(packet);
socket.close();
is there a simple way to open the channel and receive UDP datagrams on port 23000?
So, the answer was in the request, while I was struggling with receiving socket. I had to bind the sender socket to the local port used for receiving datagrams:
//send
serverAddr = InetAddress.getByName(SOULISSIP);
DatagramChannel channel = DatagramChannel.open();
sender = channel.socket();
sender.setReuseAddress(true);
//amateur hole punch
InetSocketAddress sa = new InetSocketAddress(SERVERPORT);
sender.bind(sa);
//stuff to send
List<Byte> macaco = new ArrayList<Byte>();
macaco = Arrays.asList(pingpayload);
ArrayList<Byte> buf = buildVNetFrame(macaco, prefs);
//someone help me here...
byte[] merd = new byte[buf.size()];
for (int i = 0; i < buf.size(); i++) {
merd[i] = (byte) buf.get(i);
}
packet = new DatagramPacket(merd, merd.length, serverAddr, SOULISSPORT);
sender.send(packet);
The receiver and the sender are on separate threads, using same local port. .setReuseAddress(true) permitted this.
I try to send and receive UDP packets continuously between an Android app and a server.
Is there any way to make that happen ?
My current networking code (running in a Thread) is shown bellow. The client is connected through 3G. The port configured client side is 1088.
The server just echo the packet to the client when received.
The server receive the packet correctly from the client but the client doesn't receive anything back.
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.d(TAG, "S: Connecting...");
DatagramSocket socket = new DatagramSocket();
DatagramSocket receive_socket = new DatagramSocket(SERVERPORT, InetAddress.getByName("0.0.0.0"));
while(running) {
DatagramPacket packet_send = new DatagramPacket(msg, msg.length, serverAddr, SERVERPORT);
Log.d(TAG, "C: Sending: '" + new String(msg) + "'");
socket.send(packet_send);
// Prepare a UDP-Packet that can contain the data we want to receive
DatagramPacket packet = new DatagramPacket(buf, buf.length);
Log.d(TAG, "S: Receiving...");
// Receive the UDP-Packet
receive_socket.receive(packet);
Log.d(TAG, "S: Received: '" + new String(packet.getData()) + "'");
synchronized (this) {
wait(500);
}
}
I suspect the 3G connection is NATed (the server reports a port different from 1088).
If so, is there anything I can do to overcome that ?
Or is there anything wrong I do in my code ?
Turned out the code was working fine, the 3G service provider was blocking some UDP packets.