典型的同步客户端、服务器端套接字的创建
下面的示例程序创建一个连接到服务器的客户端。该客户端是用同步套接字生成的,因此挂起客户端应用程序的执行,直到服务器返回响应为止。该应用程序将字符串发送到服务器,然后在控制台显示该服务器返回的字符串。
using System; using System.Net; using System.Net.Sockets; using System.Text; public class SynchronousSocketClient { public static void StartClient() { // Data buffer for incoming data. byte[] bytes = new byte[1024]; // Connect to a remote device. try { // Establish the remote endpoint for the socket. // This example uses port 11000 on the local computer. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()) IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000); // Create a TCP/IP socket. Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Connect the socket to the remote endpoint. Catch any errors. try { sender.Connect(remoteEP); Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()); // Encode the data string into a byte array. byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>"); // Send the data through the socket. int bytesSent = sender.Send(msg); // Receive the response from the remote device. int bytesRec = sender.Receive(bytes); Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(bytes,0,bytesRec)); // Release the socket. sender.Shutdown(SocketShutdown.Both); sender.Close(); } catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException : {0}",ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException : {0}",se.ToString()); } catch (Exception e) { Console.WriteLine("Unexpected exception : {0}", e.ToString()); } } catch (Exception e) { Console.WriteLine( e.ToString()); } } public static int Main(String[] args) { StartClient(); return 0; } }
下面的示例程序创建一个接收来自客户端的连接请求的服务器。该服务器是用同步套接字生成的,因此在等待来自客户端的连接时挂起服务器应用程序的执行。该应用程序接收来自客户端的字符串,在控制台显示该字符串,然后将该字符串回显到客户端。来自客户端的字符串必须包含字符串“<EOF>”,以发出表示消息结尾的信号。
using System; using System.Net; using System.Net.Sockets; using System.Text; public class SynchronousSocketListener { // Incoming data from the client. public static string data = null; public static void StartListening() { // Data buffer for incoming data. byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socket. // Dns.GetHostName returns the name of the // host running the application. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket. Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Bind the socket to the local endpoint and // listen for incoming connections. try { listener.Bind(localEndPoint); listener.Listen(10); // Start listening for connections. while (true) { Console.WriteLine("Waiting for a connection..."); // Program is suspended while waiting for an incoming connection. Socket handler = listener.Accept(); data = null; // An incoming connection needs to be processed. while (true) { bytes = new byte[1024]; int bytesRec = handler.Receive(bytes); data += Encoding.ASCII.GetString(bytes,0,bytesRec); if (data.IndexOf("<EOF>") > -1) { break; } } // Show the data on the console. Console.WriteLine( "Text received : {0}", data); // Echo the data back to the client. byte[] msg = Encoding.ASCII.GetBytes(data); handler.Send(msg); handler.Shutdown(SocketShutdown.Both); handler.Close(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.WriteLine("/nPress ENTER to continue..."); Console.Read(); } public static int Main(String[] args) { StartListening(); return 0; } }
典型的同步客户端、服务器端套接字的创建相关推荐
- 【Groovy】使用 Groovy 语言开发服务器 Server 和客户端 Client 套接字程序 ( 服务器客户端完整代码示例 | 运行服务器端与客户端效果及过程分析 )
文章目录 一.服务器端完整代码 Server.groovy 二.客户端完整代码 Client.groovy 三.运行服务器端与客户端效果及过程分析 一.服务器端完整代码 Server.groovy 参 ...
- 【Groovy】使用 Groovy 语言开发服务器 Server 和客户端 Client 套接字程序 ( 服务器端开发 )
文章目录 一.服务器端 ServerSocket 开发 1.创建服务器端套接字 ServerSocket 2.等待客户端请求 3.管理客户端连接 4.接收客户端数据线程 5.向客户端发送数据线程 二. ...
- 典型的异步服务器端套接字构建
下面的示例程序创建一个接收来自客户端的连接请求的服务器.该服务器是用异步套接字生成的,因此在等待来自客户端的连接时不挂起服务器应用程序的执行.该应用程序接收来自客户端的字符串,在控制台显示该字符串,然 ...
- 孙鑫MFC笔记之十四--多线程同步与异步套接字编程
线程同步有三种方式: 1. 互斥对象涉及方法: HANDLE hMutex=CreateMutex(NULL,FALSE,NULL); //第二个参数为FALSE,将互斥对象声明为空闲状态 ...
- 线程同步与异步套接字编程
1.利用事件对象来实现线程间的同步 新建一个win32 console application,取名Event,再建一个Event源文件,编辑: #include <iostream.h> ...
- MFC(线程同步与异步套接字,孙鑫C++第十六讲笔记整理)
1.事件对象:来实现线程的同步.与互斥对象一样均属于内核对象. 当人工重置有信号时,所有线程均得到信号,所以不能设为人工重置.代码就不贴了,通过创建匿名的事件对象,也可以让一个程序只能运行一个实例. ...
- 写的非常不错的一篇阻塞与非阻塞、同步与异步套接字之间的区别
当你发现自己最受欢迎的一篇blog其实大错特错时,这绝对不是一件让人愉悦的事. < IO - 同步,异步,阻塞,非阻塞 >是我在开始学习epoll和libevent的时候写的,主要的思路来 ...
- 《VC++深入详解》学习笔记 第十六章 线程同步与异步套接字编程
(颠簸喜悲幽若尽是无情人) 事件对象成员: 包含(使用计数.事件类型.事件状态) 事件类型: 人工重置的事件对象:得到通知时,等待的所有线程都变为可调度 自动重置的事件对象:得到通知时,等待的 ...
- java套接字数据对不上,服务器不从多个客户端(Java套接字)接收数据
我写了一个简单的程序,服务器应该打印多个客户端发送的数据.但服务器只接收部分数据.以下是代码的相关部分. 服务器: try { serverSocket = new ServerSocket(8888 ...
最新文章
- 链表面试题Java实现【重要】
- 【Visual Studio 2019】创建 MFC 桌面程序 ( 安装 MFC 开发组件 | 创建 MFC 应用 | MFC 应用窗口编辑 | 为按钮添加点击事件 | 修改按钮文字 | 打开应用 )
- Nis服务器主从安装配置
- Linux使用parted进行分区及拓展实验
- 设计师更高效_如何丢掉我的工作使我成为一名更好的设计师
- 自定义EditText输入框
- 索引sql server_SQL Server索引–系列介绍
- Hinton反思新作:我说反向传播不好,但还是没谁能颠覆它
- HBase MemStore和Compaction剖析
- 计算机网络自顶向下方法
- 谷歌浏览器任何页面都打不开连设置也不能打开
- 关于Mac升级系统后,出现 xcrun: error: invalid active developer path 问题的解决方案【已解决】
- 2007年高考北京满分作文:沉默的父爱
- FxFactory 7 Pro for Mac(fcpx/ae/pr视觉特效插件包)v7.2.5
- 抖音电子画册教程html,抖音爆火文字视频制作教程
- P5200 [USACO19JAN]Sleepy Cow Sorting G
- loopback address 回送地址
- HTML5中polygon坐标,SVG Polygon(多边形)
- 什么叫刀片式服务器跟其它服务器有何区别?
- 理解Servlet和Servlet容器、Web容器等概念