Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Socket Connection

Socket Connection

Scheduled Pinned Locked Moved C#
sysadminxmlhelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Agweet
    wrote on last edited by
    #1

    Hi All, i am trying to create a socket connection to a server and send it a XML string and receive the results. For some reason it does not work, here is my code: XML String: XXXXXXXXXXXXXXXX and my socket connection code: try { TcpClient socketForServer; socketForServer = new TcpClient(xxx.xxx.xxx.xxx, 6000); socketForServer.SendTimeout = 300000; socketForServer.ReceiveTimeout = 300000; NetworkStream networkStream = socketForServer.GetStream(); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); string outputString; // read the data from the host and display it streamWriter.WriteLine(XML String goes here); streamWriter.Flush(); outputString = streamReader.ReadToEnd(); networkStream.Close(); return outputString; } catch (Exception ex) { return ex.Message.ToString(); } it shows that i am connected to the server but i don't get any results back. can anyone please help me with this? thanks in advance! living life on the flip side

    F E M 3 Replies Last reply
    0
    • A Agweet

      Hi All, i am trying to create a socket connection to a server and send it a XML string and receive the results. For some reason it does not work, here is my code: XML String: XXXXXXXXXXXXXXXX and my socket connection code: try { TcpClient socketForServer; socketForServer = new TcpClient(xxx.xxx.xxx.xxx, 6000); socketForServer.SendTimeout = 300000; socketForServer.ReceiveTimeout = 300000; NetworkStream networkStream = socketForServer.GetStream(); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); string outputString; // read the data from the host and display it streamWriter.WriteLine(XML String goes here); streamWriter.Flush(); outputString = streamReader.ReadToEnd(); networkStream.Close(); return outputString; } catch (Exception ex) { return ex.Message.ToString(); } it shows that i am connected to the server but i don't get any results back. can anyone please help me with this? thanks in advance! living life on the flip side

      F Offline
      F Offline
      freakyit
      wrote on last edited by
      #2

      may be the server will not send data back to the client?!? this piece of code is not very helpfull for a good answer. first your XML you want to send is corrupt! take a look at the xml-declaration >. sencond one is that you will read data from the stream without knowing whether data is available or not ^^ use the search function to take a look at articles that work as server/client app..

      1 Reply Last reply
      0
      • A Agweet

        Hi All, i am trying to create a socket connection to a server and send it a XML string and receive the results. For some reason it does not work, here is my code: XML String: XXXXXXXXXXXXXXXX and my socket connection code: try { TcpClient socketForServer; socketForServer = new TcpClient(xxx.xxx.xxx.xxx, 6000); socketForServer.SendTimeout = 300000; socketForServer.ReceiveTimeout = 300000; NetworkStream networkStream = socketForServer.GetStream(); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); string outputString; // read the data from the host and display it streamWriter.WriteLine(XML String goes here); streamWriter.Flush(); outputString = streamReader.ReadToEnd(); networkStream.Close(); return outputString; } catch (Exception ex) { return ex.Message.ToString(); } it shows that i am connected to the server but i don't get any results back. can anyone please help me with this? thanks in advance! living life on the flip side

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        You are asking for results immediately but results would not be available immediately. You need a read loop to get the number of bytes back you expect from the server.

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

        1 Reply Last reply
        0
        • A Agweet

          Hi All, i am trying to create a socket connection to a server and send it a XML string and receive the results. For some reason it does not work, here is my code: XML String: XXXXXXXXXXXXXXXX and my socket connection code: try { TcpClient socketForServer; socketForServer = new TcpClient(xxx.xxx.xxx.xxx, 6000); socketForServer.SendTimeout = 300000; socketForServer.ReceiveTimeout = 300000; NetworkStream networkStream = socketForServer.GetStream(); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); string outputString; // read the data from the host and display it streamWriter.WriteLine(XML String goes here); streamWriter.Flush(); outputString = streamReader.ReadToEnd(); networkStream.Close(); return outputString; } catch (Exception ex) { return ex.Message.ToString(); } it shows that i am connected to the server but i don't get any results back. can anyone please help me with this? thanks in advance! living life on the flip side

          M Offline
          M Offline
          Member 790656
          wrote on last edited by
          #4

          You need to block with networkStream.Read() Otherwise the connection closes before you have the oportunity to read data. ;) See ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system/html/b46663bf-ae16-3bd8-73c3-2d057d0868e3.htm

          A 1 Reply Last reply
          0
          • M Member 790656

            You need to block with networkStream.Read() Otherwise the connection closes before you have the oportunity to read data. ;) See ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system/html/b46663bf-ae16-3bd8-73c3-2d057d0868e3.htm

            A Offline
            A Offline
            Agweet
            wrote on last edited by
            #5

            Hi All, Thanks for the answers, will take them into account and will post my results. living life on the flip side

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups