I have been looking into this matter for some time now. I have the basic concept and now trying to implement the socket communication between ASP.NET and android.
I am using the basic windows System.Net.Sockets
to work this out. I am getting a connection as I try to debug my application line by line which tells that my server is working but the problem is that the C# line gets stuck at sender.Receive(bytes)
.
This is my android side server code for TCPServer
public class TCPServer extends Thread {
public static final int SERVER_PORT = 11000;
private boolean running = false;
private PrintWriter mOut;
private OnMessageReceived messageListener;
public TCPServer(OnMessageReceived messageListener)
{
this.messageListener = messageListener;
}
public void sendMessage(String message)
{
if(mOut != null && !mOut.checkError())
{
mOut.println(message);
mOut.flush();
}
}
@Override
public void run()
{
super.run();
running=true;
try {
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
Socket client = serverSocket.accept();
try {
//sends the message to the client
mOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
//read the message received from client
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
//in this while we wait to receive messages from client (it's an infinite loop)
//this while it's like a listener for messages
while (running) {
String message = in.readLine();
if (message != null && messageListener != null) {
messageListener.messageReceived(message);
sendMessage("OK Message is received here");
//the line above and below is also not helping me with my problem
mOut.println("OK: FROM ACOM");
}
}
}
catch (Exception e)
{
e.getMessage();
}
finally {
client.close();
}
}
catch (Exception e)
{
e.getMessage();
}
}
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
This is how I call it from my main activity currently using a simple button to initiate the server to listen like this
btnStartServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Starting the server",Toast.LENGTH_LONG).show();
TCPServer tcpServer = new TCPServer(new TCPServer.OnMessageReceived() {
@Override
public void messageReceived(String message) {
txtMessageReceived.setText(message);
}
});
tcpServer.start();
}
});
Now as for the C# or ASP.NET I have just added a simple button on webforms page to test things out and this is the code for that
IPAddress ipAddress;
IPEndPoint remoteEP;
Socket sender;
byte[] bytes;
private void setUpSockets()
{
bytes = new byte[1024];
try
{
ipAddress = IPAddress.Parse("192.168.0.102");
remoteEP = new IPEndPoint(ipAddress, 11000);
sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sender.Connect(remoteEP);
byte[] msg = Encoding.ASCII.GetBytes("This is some Test <EOF>");
int byteSent = sender.Send(msg);
int byteRec = sender.Receive(bytes);
string messageRec = Encoding.ASCII.GetString(bytes, 0, byteRec);
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
catch (Exception e)
{
string exception = e.Message;
}
}
catch (Exception m)
{
string exception = m.Message;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
setUpSockets();
}
Now I am just testing it before adding it to something meaningful but I am stuck at this int byteRec = sender.Receive(bytes);
and I am not sure what should I do because as you can see I am trying to send some response from android client too. It is like it gets stuck at this line waiting for response which it never receives moreover the text box on the android side is not updating anything received as well.