Results 1 to 4 of 4

Thread: [RESOLVED] C# noob needs help getting started with network connecting

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Resolved [RESOLVED] C# noob needs help getting started with network connecting

    Hi Folks,

    A long time ago I used VB6 and have since dabbled a tiny bit with vb.net, however occasion has arisen to develop a .NET desktop program and I thought I'd use the opportunity to get started with c#.

    The program needs to talk to and listen over ethernet connections that connect to a servers, each of which have their own static IP address. The data to be sent and received consists of ASCII text strings.

    I have no idea where to start finding info to begin with this! A have searched via Google for an hour or so and most tutorials etc assume that I am building a client/server app, suggest "sockets" and then go from there... I dont really know even if this sockets is what I need as I dont have access to the server (it already has s/w on it that listens and pops what it receives out on a serial port and vice-versa).

    Last time I did anything like this I used a COM, set the port numbers etc and then just wrote data to it.... seems like what I want may have become a little more complex...

    Can someone point me in the right direction?!? Thanks
    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: C# noob needs help getting started with network connecting

    You will most likely be using members of the System.Net.Sockets namespace. Your first option should be the TcpListener and TcpClient classes. The TcpListener is used on the server side, listening for client connection requests. A TcpClient is used on the client side to request a connection. The server accepts the request and creates another TcpClient and the two then talk to each other using NetworkStreams. I have a demo in the CodeBank that does just that using asynchronous methods, but I've only created a VB version, not C#. At least you've got a place to start though.

    Note that the TcpClient is actually a wrapper for a Socket object. The TcpClient provides a simpler interface than the Socket but sometimes you need the granular control that the Socket provides. The TcpListener class can be used to create a TcpClient or a Socket when a connection request is accpted.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Re: C# noob needs help getting started with network connecting

    When you say the TcpListner on the server side, do you mean I need to set up a server to do the listening within my desktop application, or on the actual server that I am trying to connect to? As I mentioned in the first post, I dont have access to put an application on the server I wish to connect to ... Im not exactly sure how it works TBH, but the user manual specs TCP/IP protocol and a IP and that it listens for data...
    Thanks
    Thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: C# noob needs help getting started with network connecting

    If there's already server software running on the server machine then you don't have to create the server. If you were creating the server then you would use a TcpListener to receive connections from clients, whether those clients were written in .NET or not. As you're creating the client, you use a TcpClient to create a connection to a server, whether that server was written in .NET or not. The TcpListener and TcpClient are simply the .NET implementation of the TCP protocol and they will work with applications written in any language as long as they also support the TCP protocol.

    You will still have to comply with the communication protocol defined by the server though. Presumably you have access to documentation that describes the format that it expects you to provide messages and/or data in and the format that it will provide messages and/or data to you. You will need to create a layer that will translate any data that your app is working with natively between its native format and the format used by the server. That might include writing message codes and/or data lengths before writing the actual data or the like.

    As I said before, if the high-level TcpClient doesn't provide enough control then you should use the lower-level Socket instead. A TcpClient uses a Socket itself internally but hides some of the complexity. The TcpClient should be your first choice because it's simpler but, if you need that complexity to achieve your aim, use a Socket instead.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width