Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey i am using socket server in c# code and send message from the server but client can't recieve message.if i create client in c# it works fine. Please help ` public class TCPClient {

private String serverMessage;
public static final String SERVERIP = "10.0.0.121"; //your computer IP address
public static final int SERVERPORT = 8221;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;

PrintWriter out;
BufferedReader in;

/**
* Constructor of the class. OnMessagedReceived listens for the messages received from server
*/
public TCPClient(OnMessageReceived listener) {
mMessageListener = listener;
}

/**
* Sends the message entered by client to the server
* @param message text entered by client
*/
public void sendMessage(String message){
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
}
}

public void stopClient(){
mRun = false;
}

public void run() {

mRun = true;

try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVERIP);

Log.e("TCP Client", "C: Connecting...");

//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVERPORT);

try {

//send the message to the server
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

Log.e("TCP Client", "C: Sent.");

Log.e("TCP Client", "C: Done.");

//receive the message which the server sends back
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

//in this while the client listens for the messages sent by the server
while (mRun) {
serverMessage = in.readLine();

if (serverMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(serverMessage);
}
serverMessage = null;

}

Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");

} catch (Exception e) {

Log.e("TCP", "S: Error", e);

} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
}

} catch (Exception e) {

Log.e("TCP", "C: Error", e);

}

}

//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
//class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(String message);
}
} ` Thanks in advance..
Posted

1 solution

First, you should not hardcode your IP:
public static final String SERVERIP = "10.0.0.121";

instead use getLocalHost() to get your address.
Server needs ServerSocket and a thread in infinite loop to call accept()
Socket is for the client with must connect() to Server address and port (since using TCP )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900