I am very new to programming c# in visual studio. I am trying to send a message from an android app (Client side) to a windows app (Server side) using sockets. I don't know if I should be able to do it in the wireless network that I have, or is that even the problem. Also can I pick and use any port number or it has to be a specific number that I have to fetch from somewhere? I will post both my client and server codes, and the output of my server side code. Any help is much appreciated.
Client:
public class MainActivity extends Activity {
private EditText editTxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
editTxt = (EditText) findViewById(R.id.editText1);
final String str = editTxt.getText().toString();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
connectSocket(str);
}
});
}
private void connectSocket(String a){
new Thread( new Runnable() {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName("10.5.5.117");
Log.d("TCP", "C: Connecting...");
Socket socket = new Socket(serverAddr, 5555);
String message = "1";
PrintWriter out = null;
BufferedReader in = null;
try {
Log.d("TCP", "C: Sending: '" + message + "'");
out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println(message);
String text = "";
String finalText = "";
while ((text = in.readLine()) != null) {
finalText += text;
}
editTxt.setText(finalText);
Log.d("TCP", "C: Sent.");
Log.d("TCP", "C: Done.");
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.e("TCP", "C: UnknownHostException", e);
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("TCP", "C: IOException", e);
e.printStackTrace();
}
}}).start();
}
}
Server:
namespace WindowsSocketServer
{
public class serv
{
public static void Main()
{
try
{
IPAddress ipAd = IPAddress.Parse("10.5.5.117");
// use local m/c IP address, and
// use the same in the client
// Initializes the Listener
TcpListener myList = new TcpListener(ipAd, 5555);
// Start Listeneting at the specified port
myList.Start();
Console.WriteLine("The server is running at port 5555...");
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
m:
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
char cc = ' ';
string test = null;
Console.WriteLine("Recieved...");
for (int i = 0; i < k - 1; i++)
{
Console.Write(Convert.ToChar(b[i]));
cc = Convert.ToChar(b[i]);
test += cc.ToString();
}
switch (test)
{
case "1":
break;
}
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
s.Close();
// clean up
goto m;
s.Close();
myList.Stop();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
}
Server Output:
The thread 'vshost.NotifyLoad' (0x21e8) has exited with code 0 (0x0).
The thread '<No Name>' (0x1d04) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x2448) has exited with code 0 (0x0).
'WindowsSocketServer.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Projects\Offline\WindowsSocketServer\bin\Debug\WindowsSocketServer.exe', Symbols loaded.
'WindowsSocketServer.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50 a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Error..... at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()
at WindowsSocketServer.serv.Main() in
C:\Projects\Offline\WindowsSocketServer\Program.cs:line 27
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
The thread 'vshost.RunParkingWindow' (0x1a5c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1ba4) has exited with code 0 (0x0).
The program '[8828] WindowsSocketServer.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).