1 using System;
2 using System.Net;
3 using System.Net.Sockets;
4 using System.Text;
5
6 public class SynchronousSocketClient {
7
8 public static void StartClient() {
9 // Data buffer for incoming data.
10 byte[] bytes = new byte[1024];
11
12 // Connect to a remote device.
13 try {
14 // Establish the remote endpoint for the socket.
15 // This example uses port 11000 on the local computer.
16             IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
17             IPAddress ipAddress = ipHostInfo.AddressList[0];
18             IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
19
20 // Create a TCP/IP  socket.
21             Socket sender = new Socket(AddressFamily.InterNetwork, 
22                 SocketType.Stream, ProtocolType.Tcp );
23
24 // Connect the socket to the remote endpoint. Catch any errors.
25 try {
26                 sender.Connect(remoteEP);
27
28                 Console.WriteLine("Socket connected to {0}",
29                     sender.RemoteEndPoint.ToString());
30
31 // Encode the data string into a byte array.
32 byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
33
34 // Send the data through the socket.
35 int bytesSent = sender.Send(msg);
36
37 // Receive the response from the remote device.
38 int bytesRec = sender.Receive(bytes);
39                 Console.WriteLine("Echoed test = {0}",
40                     Encoding.ASCII.GetString(bytes,0,bytesRec));
41
42 // Release the socket.
43                 sender.Shutdown(SocketShutdown.Both);
44                 sender.Close();
45
46             } catch (ArgumentNullException ane) {
47                 Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
48             } catch (SocketException se) {
49                 Console.WriteLine("SocketException : {0}",se.ToString());
50             } catch (Exception e) {
51                 Console.WriteLine("Unexpected exception : {0}", e.ToString());
52             }
53
54         } catch (Exception e) {
55             Console.WriteLine( e.ToString());
56         }
57     }
58
59 public static int Main(String[] args) {
60         StartClient();
61 return 0;
62     }
63 }

服务器端:

1 using System;
2 using System.Net;
3 using System.Net.Sockets;
4 using System.Text;
5
6 public class SynchronousSocketListener {
7
8 // Incoming data from the client.
9 public static string data = null;
10
11 public static void StartListening() {
12 // Data buffer for incoming data.
13 byte[] bytes = new Byte[1024];
14
15 // Establish the local endpoint for the socket.
16 // Dns.GetHostName returns the name of the 
17 // host running the application.
18         IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
19         IPAddress ipAddress = ipHostInfo.AddressList[0];
20         IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
21
22 // Create a TCP/IP socket.
23         Socket listener = new Socket(AddressFamily.InterNetwork,
24             SocketType.Stream, ProtocolType.Tcp );
25
26 // Bind the socket to the local endpoint and 
27 // listen for incoming connections.
28 try {
29             listener.Bind(localEndPoint);
30             listener.Listen(10);
31
32 // Start listening for connections.
33 while (true) {
34                 Console.WriteLine("Waiting for a connection");
35 // Program is suspended while waiting for an incoming connection.
36                 Socket handler = listener.Accept();
37                 data = null;
38
39 // An incoming connection needs to be processed.
40 while (true) {
41                     bytes = new byte[1024];
42 int bytesRec = handler.Receive(bytes);
43                     data += Encoding.ASCII.GetString(bytes,0,bytesRec);
44 if (data.IndexOf("<EOF>") > -1) {
45 break;
46                     }
47                 }
48
49 // Show the data on the console.
50                 Console.WriteLine( "Text received : {0}", data);
51
52 // Echo the data back to the client.
53 byte[] msg = Encoding.ASCII.GetBytes(data);
54
55                 handler.Send(msg);
56                 handler.Shutdown(SocketShutdown.Both);
57                 handler.Close();

.NET SOCKET通信编程的更多相关文章

  1. 【转】C# Socket通信编程

    https://www.cnblogs.com/dotnet261010/p/6211900.html#undefined 一:什么是SOCKET socket的英文原义是“孔”或“插座”.作为进程通 ...

  2. linux系统socket通信编程实践

    简单介绍并实现了基于UDP(TCP)的windows(UNIX下流程基本一致)下的服务端和客户端的程序,本文继续探讨关于UDP编程的一些细节. 下图是一个简单的UDP客户/服务器模型: 我在这里也实现 ...

  3. linux系统socket通信编程详解函数

    linux socket编程之TCP与UDP   TCP与UDP区别 TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之 ...

  4. linux系统UDP的socket通信编程3

    我刚开始接触linux下的socket编程,边抄边理解udp socket编程,我的疑问是server不指定IP地址,client的目标IP地址是127.0.0.1,这样就可以通信吗?在同一主机下是不 ...

  5. linux系统socket通信编程2

    一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...

  6. linux系统socket通信编程1

    Linux下的Socket编程大体上包括Tcp Socket.Udp Socket即Raw Socket这三种,其中TCP和UDP方式的Socket编程用于编写应用层的socket程序,是我们用得比较 ...

  7. C#进行Socket通信编程之一

    关于Socket编程的相关资料(含实例)在网上多如牛毛,而我写这篇文章的初衷仅仅是为了记录自己的一些心得体会. Socket提供了这样一个接口,可以方便地使程序员通过其来发送和接收网络上的数据.在利用 ...

  8. linux系统UDP的socket通信编程2

    UDP套接字编程范例: server端代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...

  9. linux系统UDP的socket通信编程

    发送方: /* * File: main.c * Author: tianshuai * * Created on 2011年11月29日, 下午10:34 * * 主要实现:发送20个文本消息,然后 ...

随机推荐

  1. 使用java8

    刚开始一直使用的jdk7,但是学习spark的时候spark推荐Jdk8,很多示例程序都用到了lambda 机器上安装jdk8只是实验用途,默认还想使用jdk7 安装完后 网上说这是因为jdk8安装的 ...

  2. 多线程和并发管理 .NET多线程服务

    线程相关静态变量 默认静态变量应用程序域所有线程可见.如果静态变量需要在线程间共享,同步访问也就必然了. 线程相关静态变量保证线程安全,同一时间只有一个线程可访问,且每个线程都有该静态变量的拷贝. p ...

  3. .net对各表的操作详细到字段的更改记录的日志

    存入数据库中,目前的字段包括操作人,操作时间,sql语句,被修改的字段,字段原值,操作人的身份. /// <summary> /// 添加操作日志 /// </summary> ...

  4. TQ210开发板NFS挂载android4.0.4的rootfs的方法

    首先声明的是,我使用的u-boot是自己移植的u-boot2013.01.01而非天嵌官方的那个,至于使用官方的u-boot如何去实现nfs挂载rootfs我没怎么研究过,不过原理方法都是一致的. 主 ...

  5. 通过maven添加quartz

    pom.xml中相关dependency信息 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <depen ...

  6. ubuntu server 系统,更换阿里云源(用户更新源)

    Ubuntu安装完毕后,默认使用的是官方的源,在国内访问速度很慢,这里介绍更换为阿里云的源方法. 步骤如下: 1.备份源配置文件 sudo cp /etc/apt/sources.list /etc/ ...

  7. Learn Vim

    Vim Note 很早就知道vim是一个很强大的编辑器,也用了很久.不过没有系统的总结过,这次就写个笔记方便以后看看(本文在vim下编辑完成) 第一印象 打开vim第一感觉就是无从下手,相信大多数人和 ...

  8. log4net日志文件名输出格式化

    日志文件输出格式: 20130626.txt20130627.txt20130628.txt20130629.txt <appender name="rolling" typ ...

  9. 为什么用服务不用线程-Android

    1.什么是Service? A Service is an application component representing either an application's desire to p ...

  10. 五、PackageManager获取版本号

    PackageInfo代表的是关于一个包的所有信息,就相当于一个APP应用的清单文件中收集到的所有信息. 通过这个类我们就可以获取类似版本号等一些信息. 1 2 3 4 5 6 7 8 9 10 11 ...