Results 1 to 5 of 5

Thread: [RESOLVED] [VS2010] simplest tcp/ip client and server

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    36

    Resolved [RESOLVED] [VS2010] simplest tcp/ip client and server

    What i need it to send a stream of data, the data will consist of 3 numbers, i have tryied finding a sample but they dont work on vs2010, have no idea why, will continue searching.

    I am new to c# but i like it a lot coz it's very readable, and similar to VB which i am a bit more familiar with.

    The stream can be send as a string also and i can probably make a reader figure out the numbers in the string.

    Anyhow any help is appreciated, just take in mind i am a beginner, Thank you.

  2. #2
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [VS2010] simplest tcp/ip client and server

    1) What examples have you tried? What "doesnt" work?
    2) the MSDN documentation provides loads of examples.... they work 100% all the time.

    hint: take a look at TCPClient, TCPListener and NetworkStream classes.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  3. #3
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: [VS2010] simplest tcp/ip client and server

    Hi,

    There are tons of examples in the net...I think there are samples too at the
    codebank section of this site..


    Greg
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    36

    Re: [VS2010] simplest tcp/ip client and server

    here is the Client part:

    C# Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Net.Sockets;
    6.  
    7. namespace tcp_Klijent
    8. {
    9.     class Program
    10.     {
    11.         static void Main(string[] args)
    12.         {
    13.  
    14.  
    15.             TcpClient tcpClient = new TcpClient();
    16.             tcpClient.Connect("127.0.0.1" , 3142);
    17.             if (tcpClient.Connected)
    18.             {
    19.                     NetworkStream NS =tcpClient.GetStream();
    20.             while (NS.CanWrite)
    21.             {
    22.                     Console.WriteLine ("unesi tekst");
    23.                      a:
    24.  
    25.                      Byte[] data = Encoding.ASCII.GetBytes(Console.ReadLine());
    26.  
    27.                        NS.Write(data, 0, data.Length);
    28.                       goto a;
    29.  
    30.  
    31.  
    32.             }
    33.  
    34.  
    35.             }
    36.         }
    37.     }
    38. }

    and here is the server part:

    C# Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Net.Sockets;
    6. using System.IO;
    7. using System.Net;
    8.  
    9.  
    10.  
    11. namespace tcp_server
    12. {
    13.     class Program
    14.     {
    15.         static void Main(string[] args)
    16.         {
    17.             Int32 port = 3142;
    18.             IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    19.  
    20.             TcpListener tcpListener = new TcpListener(localAddr , port);
    21.  
    22.             tcpListener.Start();
    23.             for (; ; )
    24.                 {
    25.                     TcpClient tcpClient = tcpListener.AcceptTcpClient();
    26.                     if (tcpClient.Connected)
    27.                     {
    28.  
    29.                         NetworkStream NS = tcpClient.GetStream();
    30.                     a:
    31.                         StringBuilder SB = new StringBuilder();
    32.                         Byte[] a = new Byte[1024];
    33.                         int b = 0;
    34.  
    35.                         do
    36.                         {
    37.  
    38.                             b = NS.Read(a, 0, a.Length);
    39.                             SB.AppendFormat("{0}" , Encoding.ASCII.GetString(a,0,b));
    40.  
    41.  
    42.                        
    43.                         }
    44.  
    45.                         while (NS.DataAvailable);
    46.                        
    47.                         Console.WriteLine("poruka klijenta:  " + SB);
    48.                         goto a;
    49.                    
    50.                     }
    51.  
    52.  
    53.            
    54.            
    55.                 }
    56.  
    57.  
    58.         }
    59.     }
    60. }

    Thank you for the hints, hopefully this will be helpful to someone

  5. #5
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [RESOLVED] [VS2010] simplest tcp/ip client and server

    never use goto....

    the examples on MSDN are the ones to really follow

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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