Server Side

If you have understood whatever I have described so far, you will easily understand the Server part of the socket application. So far we have been talking about a client making connection to a server and sending and receiving data.

On the Server end, the application has to send and receive data. But in addition to adding and receiving data, server has to allow the clients to make connections by listening at some port. Server does not need to know client I.P. addresses. It really does not care where the client is because its not the server but client who is responsible for making connection. Server's responsibility is to manage client connections.

On the server side there has to be one socket called the Listener socket that listens at a specific port number for client connections. When the client makes a connection, the server needs to accept the connection and then in order for the server to send and receive data from that connected client it needs to talk to that client through the socket that it got when it accepted the connection. The following code illustrates how server listens to the connections and accepts the connection:

public Socket m_socListener;
public void
StartListening()
{
    try
    {
        //create the listening
socket...
        m_socListener = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
     
  IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,8221);
        //bind
to local IP Address...
        m_socListener.Bind( ipLocal );
       
//start listening...
        m_socListener.Listen (4);
        // create
the call back for any client connections...
       
m_socListener.BeginAccept(new AsyncCallback ( OnClientConnect ),null);
     
  cmdListen.Enabled = false;
    }
    catch(SocketException se)
   
{
        MessageBox.Show ( se.Message );
    }
}

If you look at the above code carefully you will see that its similar to we
did in the asynchronous client. First of all the we need to create a listening
socket and bind it to a local IP address. Note that we have given Any as the
IPAddress (I will explain what it means later), and we have passed the port
number as 8221. Next we made a call to Listen function. The 4 is a
parameter indicating backlog indicating the maximum length of the queue of
pending connections.

Next we made a call to BeginAccept passing it a delegate
callback. BeginAccept is a non-blocking method that returns
immediately and when a client has made requested a connection, the callback
routine is called and you can accept the connection by calling
EndAccept. The EndAccept returns a socket object which
represents the incoming connection. Here is the code for the callback
delegate:

public void OnClientConnect(IAsyncResult asyn)
{
   
try
    {
        m_socWorker = m_socListener.EndAccept (asyn);
     
  WaitForData(m_socWorker);
    }
    catch(ObjectDisposedException)
 
  {
        System.Diagnostics.Debugger.Log(0,"1","\n OnClientConnection:
Socket has been closed\n");
    }
    catch(SocketException se)
   
{
        MessageBox.Show ( se.Message );
    }
}

Here we accept the connection and call WaitForData which in turn
calls BeginReceive for the m_socWorker.

If we want to send data some data to client we use m_socWorker socket for
that purpose like this:

Object objData = txtDataTx.Text;
byte[] byData =
System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
m_socWorker.Send
(byData);

Socket Programming in C#--Server Side的更多相关文章

  1. C Socket Programming for Linux with a Server and Client Example Code

    Typically two processes communicate with each other on a single system through one of the following ...

  2. [PHP-Socket] Socket Programming in PHP

    Simple Client-Server socket program in PHP Introduction Sockets are used for interprocess communicat ...

  3. Socket Programming in C#--Conclusion

    Conclusion And that's all there is to it! Here is how our client looks like Here is how our server l ...

  4. Socket Programming in C#--Getting Started

    Getting Started You can argue that one can overcome these shortcomings by multithreading meaning tha ...

  5. How To: Perl TCP / UDP Socket Programming using IO::Socket::INET

    http://www.thegeekstuff.com/2010/07/perl-tcp-udp-socket-programming/ In this article, let us discuss ...

  6. Socket programming in C on Linux | tutorial

    TCP/IP socket programming This is a quick guide/tutorial to learning socket programming in C languag ...

  7. Python Socket Programming

    本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下. Python的sock ...

  8. linux c socket programming

    原文:linux c socket programming http://54min.com/post/http-client-examples-using-c.html 好文章 PPT http:/ ...

  9. TCP Socket Programming in Node.js

    TCP Socket Programming in Node.js Posted on October 26th, 2011 under Node.jsTags: Client, node.js, S ...

随机推荐

  1. 利用UIScrollView实现几个页面的切换

    此实例可以了解一下UIScrollView的运用,以及表格跟页面跳转的内容: 原作者地址:http://www.cocoachina.com/bbs/read.php?tid=323514 效果图如下 ...

  2. IOS开发-表单控件的应用

    1. 需求描述 2. 开发环境介绍 3. 创建一个工程 4. 工程配置介绍 5. 目录结构介绍 6. 界面设置 7. 关联输入输出 8. 关联事件代码 9. 运行结果 10. UITextField ...

  3. Xcode找Library位置

  4. yum命令指南-yum使用方法

    yum check-update  检查可更新的所有软件包    yum update  下载更新系统已安装的所有软件包    yum upgrade  大规模的版本升级,与yum update不同的 ...

  5. TOMCAT报错:HTTP Status 404 -

    构建struts2工程师,tomcat报错: HTTP Status 404 - type Status report message description The requested resour ...

  6. 【mysql】一个关于order by排序的问题

    I have a table CREATE TABLE `tableMain` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value1` varchar(45) ...

  7. 烂泥:ubuntu安装vmtools

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近由于工作需要,需要使用桌面版的linux系统,所以就选择了ubuntu.同时为了方便使用,就在VM中安装ubuntu. 但是为了文件以及操作的方便就 ...

  8. 版本控制工具VSS使用介绍

    什么是版本控制? 1.怎样对研发项目进行整体管理 2.项目开发小组的成员之间如何以一种有效的机制进行协调 3.如何进行对小组成员各自承担的子项目的统一管理 4.如何对研发小组各成员所作的修改进行统一汇 ...

  9. js清除缓存方法

    1.加入如下头描述 <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUI ...

  10. 终于完成了Josephus的C语言实现啦~~

    /*以下程序用来解决Josephus问题,现在只是完成了M>N的情况,2015-08-20 22:22:20*//*发现一个问题:数组的赋值问题:char People[N]={1};并不代表所 ...