Socket Programming in C#--Server Side
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的更多相关文章
- 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 ...
- [PHP-Socket] Socket Programming in PHP
Simple Client-Server socket program in PHP Introduction Sockets are used for interprocess communicat ...
- 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 ...
- Socket Programming in C#--Getting Started
Getting Started You can argue that one can overcome these shortcomings by multithreading meaning tha ...
- 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 ...
- Socket programming in C on Linux | tutorial
TCP/IP socket programming This is a quick guide/tutorial to learning socket programming in C languag ...
- Python Socket Programming
本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下. Python的sock ...
- linux c socket programming
原文:linux c socket programming http://54min.com/post/http-client-examples-using-c.html 好文章 PPT http:/ ...
- TCP Socket Programming in Node.js
TCP Socket Programming in Node.js Posted on October 26th, 2011 under Node.jsTags: Client, node.js, S ...
随机推荐
- R语言学习笔记:向量
向量是R语言最基本的数据类型. 单个数值(标量)其实没有单独的数据类型,它只不过是只有一个元素的向量. x <- c(1, 2, 4, 9) x <- c(x[1:3], 88, x[4] ...
- HorizontalScrollView实现先左滑动后右滑动动画
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s ...
- SQL语句的简单使用
首先要先引入libsqlite3.0.tbd框架 DataBaseHandle.h #import <Foundation/Foundation.h> @interface DataBas ...
- iOS 自定义进度条
自定义条形进度条(iOS) ViewController.m文件 #import "ViewController.h" @interface ViewController () @ ...
- 关灯游戏源码(iOS)
就是点一下灯 它本身和周围4盏灯会变色 ViewController.m文件 #import "ViewController.h" #import "UIView+cha ...
- c语言中的部分字符串和字符函数
// // main.c // homeWork1230 // // #include <stdio.h> #include <string.h> #include <c ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Maven学习遇到的第一个错误
现在开始学习maven,我是在极客学院看的视频学习的,学习当然会遇到异常,下面是我遇到的第一个异常 [ERROR] Failed to execute goal org.apache.maven.pl ...
- 问题解决——SolidWorks 已停止工作 (Windows7 + SolidWorks 2010 SP0.0)
给同事的SolidWorks解决问题时偶然间发现的. -------------------------------------------------------------- 本文原创,转载请注明 ...
- UVA 10827 Maximum sum on a torus (LA)
算法入门经典训练指南88页练习 ::这道题只要把原矩阵扩大4倍,那么其跟最大子矩阵的题目就很类似,把二维转化成一维,求最大的序列和,不过这个序列的长度不能超过n. 长度不能超过n? 那这道题又跟hdu ...