I am trying to write a simple client server program. Both client and server is programmed in java and connected via Access Point as Local Area Network, the server is my laptop(computer) and the client is my android device. Here is code for the client side (android):
package com.caisar.andronet;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
private Button mSendButton;
private EditText ipInput;
private EditText portInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSendButton = (Button) findViewById(R.id.button1);
ipInput = (EditText) findViewById(R.id.editText1);
portInput = (EditText) findViewById(R.id.editText2);
mSendButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
sendPacket();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void sendPacket() throws IOException
{
int port = Integer.parseInt(portInput.getText().toString());
InetAddress address = InetAddress.getByName(ipInput.getText().toString());
String message = "Ini adalah caisar oentoro";
byte[] messages = message.getBytes();
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(messages, messages.length, address, port);
socket.send(packet);
socket.close();
}
}
And this is the server program:
import java.net.*;
import java.io.*;
class DatagramReceiver{
public static void main(String[] args){
try
{
int MAX_LEN = 40;
int localPort = Integer.parseInt(args[0]);
DatagramSocket socket = new DatagramSocket(localPort);
byte[] buffer = new byte[MAX_LEN];
DatagramPacket packet = new DatagramPacket(buffer, MAX_LEN);
socket.receive(packet);
String message = new String(buffer);
System.out.println(message);
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
The problem is, when trying the server program on Ubuntu, it works well without any trouble, but when I tried that on Windows, the server program didn’t show any response. So what is the trouble that ‘blocks’ the server from listening or accepting data sent from client?
Instead of turning off the whole firewall, you can open the specific port(s) used for UDP (or TCP is that is what you are using) This turns off the firewall just for the port you need.