1. Belal Shakhatrah's Avatar
    Hi everybody,
    I'm trying to write code to communicate between c# server and Android client by using TCP socket, I'm using below code but it dosn't work


    C# Server Code:
    Code:
                  TcpListener tcpListener = new TcpListener(sampleTcpPort);
                    while (true)
                    {
                        tcpListener.Start();
                        //Program blocks on Accept() until a client connects.
                        Socket soTcp = tcpListener.AcceptSocket();
                        Console.WriteLine("SampleClient is connected through TCP.");
                        Byte[] received = new Byte[1024];
                        int bytesReceived = soTcp.Receive(received, received.Length, 0);
                        String dataReceived = System.Text.Encoding.ASCII.GetString(received);
                        Console.WriteLine(dataReceived);
                        String returningString = "The Server got your message through TCP: " + dataReceived;
                        Byte[] returningByte = System.Text.Encoding.ASCII.GetBytes(returningString.ToCharArray());
                        //Returning a confirmation string back to the client.
                        soTcp.Send(returningByte, returningByte.Length, 0);
                        tcpListener.Stop();
                    }



    Android Client Code:
    Code:
         class testClass 
    {
    	private String serverIpAddress = "127.0.0.1";
    	
    	public String results="";
    	
    	public testClass()
    	{
    		Thread cThread = new Thread(new ClientThread());
            cThread.start();
    	}
    	
    	
    	public class ClientThread implements Runnable 
    	{
    	    public void run() 
    	    {
    	        try 
    	        {
    
    	            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
    	            Log.d("ClientActivity", "C: Connecting...");	            
    	            while (true) 
    	            {
    	        		results="";
    	                try 
    	                {
    	                	Socket socket = new Socket("localhost", 4567);
    	                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    	                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    	                    out.write("Test");
    	                    out.flush();
    	                    String inMsg = "";
    	                    boolean b=false;
    	                    while (!b)
    	                    {
    	                       inMsg = in.readLine();
    	                       if(inMsg!="")
    	                    	   b=true;
    	                    }
    	    	            socket.close();
    	    	            Log.d("ClientActivity", "C: Closed.");
    
    	                } catch (Exception e) {Log.e("ClientActivity", "S: Error", e);}
    	            }
    	        } 
    	        catch (Exception e) { Log.e("ClientActivity", "C: Error", e);}
    	    }
    	}
    
    }
    03-10-2013 07:44 PM
  2. Belal Shakhatrah's Avatar
    where is the fu*****ck replies
    03-12-2013 09:23 AM

Similar Threads

  1. Help please: Syncing contacts between Android and Hotmail
    By TGBmark1234 in forum General Help and How To
    Replies: 11
    Last Post: 12-23-2013, 11:36 AM
  2. Video Chatting between Android and iPad?
    By Kelemvor33 in forum Android Apps
    Replies: 1
    Last Post: 12-11-2011, 10:26 PM
  3. Video chat between android and iPhone
    By abocornwall in forum The Android Central Lounge
    Replies: 3
    Last Post: 08-08-2011, 05:31 PM
  4. Switching SIMs between Android and BlackBerry
    By Moscow Desire in forum General Help and How To
    Replies: 7
    Last Post: 08-04-2011, 07:03 AM
  5. Why the disparity between Android and iPhone games?
    By Libuff in forum Android Games
    Replies: 8
    Last Post: 01-18-2010, 03:44 PM
LINK TO POST COPIED TO CLIPBOARD