Home > Programming > C# TCP/IP Client Server example code.

C# TCP/IP Client Server example code.

December 5th, 2009 Jimmy Leave a comment Go to comments

Below is example code written in C# for a TCP/IP client and server. The TCP server uses what is called a Listener object, which listens for incoming connections. On the client side, the code is written to connect to a specific IP address at which point the server side makes a connection and both client and server can talk to each other. If your working in a DLL project in either Mono, Visual Studio or Sharp Develop, you can compiles this into an application library (DLL File).

Server Side Code:

using System;

using System.Net.Sockets;

public class AsynchIOServer

{

public static void Main()

{

TCPListener tcpListener = new TCPListener(10);

tcpListener.Start();

Socket socketForClient = tcpListener.Accept();

if (socketForClient.Connected)

{

Console.WriteLine("Client connected");

NetworkStream networkStream = new NetworkStream(socketForClient);

System.IO.StreamWriter streamWriter =

new System.IO.StreamWriter(networkStream);

System.IO.StreamReader streamReader =

new System.IO.StreamReader(networkStream);

string theString = "Sending";

streamWriter.WriteLine(theString);

Console.WriteLine(theString);

streamWriter.Flush();

theString = streamReader.ReadLine();

Console.WriteLine(theString);

streamReader.Close();

networkStream.Close();

streamWriter.Close();

}

socketForClient.Close();

Console.WriteLine("Exiting...");

}

}

 

Client Code:

using System;

using System.Net.Sockets;

public class Client

{

static public void Main( string[] Args )

{

TCPClient socketForServer;

try

{

socketForServer = new TCPClient("localHost", 10);

}

catch

{

Console.WriteLine(

"Failed to connect to server at {0}:999", "localhost");

return;

}

NetworkStream networkStream = socketForServer.GetStream();

System.IO.StreamReader streamReader =

new System.IO.StreamReader(networkStream);

System.IO.StreamWriter streamWriter =

new System.IO.StreamWriter(networkStream);

try

{

string outputString;

// read the data from the host and display it

{

outputString = streamReader.ReadLine();

Console.WriteLine(outputString);

streamWriter.WriteLine("Client Message");

Console.WriteLine("Client Message");

streamWriter.Flush();

}

}

catch

{

Console.WriteLine("Exception reading from Server");

}

// tidy up

networkStream.Close();

}

}

 

Categories: Programming Tags: ,
Digg: DIGG ME
  1. December 30th, 2009 at 05:48 | #1

    hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I

    was with the information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful

    for .Net community.

    There is a good C# resource site, Have alook

    http://www.csharptalk.com

    simi

  2. March 23rd, 2010 at 02:08 | #2

    Is this multi client supproted???

  3. March 23rd, 2010 at 13:30 | #3

    no.

  1. No trackbacks yet.