Search This Blog

  • ()
  • ()
Show more

C# Server Socket program

C# Server Socket program The C# Socket Programming has two sections. 1. C# Server Socket Program 2. C# Client Socket Program Server Socket ...

C# Server Socket program


The C# Socket Programming has two sections.


1. C# Server Socket Program


2. C# Client Socket Program



csharp-server-socket-program

Server Socket Program


The Server Socket Program here is a C# Console based Application . This program act as a Server and listening to clients request . Here we assign a Port No. 8888 for the Server Socket , it is an instance of the C# Class TcpListener , and call itsstart() method.



  TcpListener serverSocket = new TcpListener(8888);

serverSocket.Start();


The next step is to create an infinite loop for monitoring the request from Client's side . When the Server Socket accept a request from the Client side, it reads the data from NetworkStream and also it write the response to NetworkStream .


From the following C# program you can understand how to create a Socket Server in C# . Create a new C# Console Application Project and put the following source code into the project.




 


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

TcpListener serverSocket = new TcpListener(8888);
int requestCount = 0;
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
Console.WriteLine(" >> Server Started");
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> Accept connection from client");
requestCount = 0;

while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> Data from client - " + dataFromClient);
string serverResponse = "Last Message from client" + dataFromClient;
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> exit");
Console.ReadLine();
}

}
}


How to run this program ?
The C# Socket Programming has two sections.

1. C# Server Socket Program

2. C# Client Socket Program

When you finish coding and build the Server and Client program , First you have to start C# Server Socket Program from DOS prompt , then you will get a message " Server Started " in your DOS screen, where the server program is running .

Next step is to start C# Client Socket Program in the same computer or other computers on the same network . When you start the client program , it will establish a connection to the Server and get a message in client screen " Client Started " , at the same time you can see a message in the Server screen " Accept connection from client " .

Now your C# Server Socket Program and C# Client Socket Program is get connected and communicated . If you want to communicate the Server and Client again , click the button in the client program , then you can see new messages in the Server and Client programs displayed.

COMMENTS

Name

apple,2,Article,14,At home,13,Author,14,Beauty,1,Biography,2,blackberry,1,Business,6,Cars,5,Celebrity,13,conspiraci,15,Fashion,5,galaxy,1,Gallery,1,Games,11,google,1,Hair,1,Health amp; Fitness,2,Histori,11,Home,169,HOSTING,27,HTML,7,imac,1,Image,1,iphone,1,Itcyber,7,Kuran,31,Lajme,34,Life amp; Love,1,Makeup amp; Skincare,1,mobile,5,monitor,1,Movies,2,News,5,No category,5,Photography,6,PHP,30,Poezi,24,POEZI amp; TEKSTE,2,Post,14,PROGRAMMIM,46,Relationships,1,review,6,samsung,1,Seo,7,Softvare,4,Sport,5,Sports,7,Stars,5,Tag,7,Tags,7,Tech,30,Teknologji,37,THEMES,3,Tutorials,191,Video,47,Videos,4,Vip News,2,Webhosting,67,WordPress,7,World,13,
ltr
item
Cr337: C# Server Socket program
C# Server Socket program
http://csharp.net-informations.com/communications/img/csharp-server-socket-program.JPG
Cr337
https://cr337.blogspot.com/2016/04/c-server-socket-program.html
https://cr337.blogspot.com/
http://cr337.blogspot.com/
http://cr337.blogspot.com/2016/04/c-server-socket-program.html
true
5516280490839897951
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy