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. Question About Sockets

Question About Sockets

Scheduled Pinned Locked Moved C#
sysadminhelpquestionlounge
3 Posts 2 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.
  • J Offline
    J Offline
    jtmtv18
    wrote on last edited by
    #1

    i have a quick question about getting sockets to work... im trying to write a simple chat program with a client side program and a server (Async not block sockets). i seem to beable to get the client to connect to the server but i cant get the servers Socket.BeginRecieve method to work correctly it always returns me with a error among the lines of. "The attempted operation is not supported by the object referenced." The code i use for the sockets is as follows. "Please note that if it is to hard to read here i would be more then happy to send the file. They are both VERY small" SentBytes = new Byte[22]; listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); IPAddress hostadd = Dns.Resolve("MyIpIsHere").AddressList[0]; IPEndPoint EPhost = new IPEndPoint(hostadd, 8000); listener.Bind(EPhost); listener.Listen(22); // listener.BeginAccept(new AsyncCallback(AcceptCallBack),listener); //the line below is where the error is. listener.BeginReceive(SentBytes,0,15,SocketFlags.MaxIOVectorLength,new AsyncCallback(Reciever),listener); //and my Call back method looks like this > private void Reciever(IAsyncResult e) { string f = ASCIIEncoding.ASCII.GetString(SentBytes); MessageBox.Show(f); listener.EndReceive(e); } Any ideas would be great. Thanks Alot Jesse M The Code Project Is Your Friend...

    B 1 Reply Last reply
    0
    • J jtmtv18

      i have a quick question about getting sockets to work... im trying to write a simple chat program with a client side program and a server (Async not block sockets). i seem to beable to get the client to connect to the server but i cant get the servers Socket.BeginRecieve method to work correctly it always returns me with a error among the lines of. "The attempted operation is not supported by the object referenced." The code i use for the sockets is as follows. "Please note that if it is to hard to read here i would be more then happy to send the file. They are both VERY small" SentBytes = new Byte[22]; listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); IPAddress hostadd = Dns.Resolve("MyIpIsHere").AddressList[0]; IPEndPoint EPhost = new IPEndPoint(hostadd, 8000); listener.Bind(EPhost); listener.Listen(22); // listener.BeginAccept(new AsyncCallback(AcceptCallBack),listener); //the line below is where the error is. listener.BeginReceive(SentBytes,0,15,SocketFlags.MaxIOVectorLength,new AsyncCallback(Reciever),listener); //and my Call back method looks like this > private void Reciever(IAsyncResult e) { string f = ASCIIEncoding.ASCII.GetString(SentBytes); MessageBox.Show(f); listener.EndReceive(e); } Any ideas would be great. Thanks Alot Jesse M The Code Project Is Your Friend...

      B Offline
      B Offline
      Burt Harris
      wrote on last edited by
      #2

      Hello Jesse, You are trying to call BeginReceive on the wrong socket. The socket you set up with the Listen() and BeginAccept() requests will simply deliver callbacks to you when clients attempt to connect. In your AcceptCallBack function, you must call EndAccept(), which will return to you a new socket connected to that client. You would then call BeginReceive() on that new socket. Also, in your Receiver() function, you should use this new socket to call EndReceive() on. The return value from this is the number of bytes you actually received (which may be smaller than you requested.) Note also that if something goes wrong with the network connection, the EndReceive() function will throw an exception, you should be prepaired to handle that. Hope that helps. Burt Harris

      J 1 Reply Last reply
      0
      • B Burt Harris

        Hello Jesse, You are trying to call BeginReceive on the wrong socket. The socket you set up with the Listen() and BeginAccept() requests will simply deliver callbacks to you when clients attempt to connect. In your AcceptCallBack function, you must call EndAccept(), which will return to you a new socket connected to that client. You would then call BeginReceive() on that new socket. Also, in your Receiver() function, you should use this new socket to call EndReceive() on. The return value from this is the number of bytes you actually received (which may be smaller than you requested.) Note also that if something goes wrong with the network connection, the EndReceive() function will throw an exception, you should be prepaired to handle that. Hope that helps. Burt Harris

        J Offline
        J Offline
        jtmtv18
        wrote on last edited by
        #3

        Thanks alot burt, I was looking around on the microsoft website and just now figured out the problem. I noticed in vs.nets intelisence that it said it would hand you a new socket but i didnt know what to do with them. thanks alot for clearing that up for me. I got the program to work (i wouldnt say work good though) now i just need to get it to work 2 way *send / recieve* now. Thanks agian. Jesse M p.s : How is it working for microsoft ? I live about 2 hours from redmond in Wa. Im currently getting my mcsd / bachelors in college right now... hoping to someday work for microsoft. The Code Project Is Your Friend...

        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