C#.net网络通信问题。我在我的电脑上存了一组数据,想把这组数据发送到另外一台电脑(这台电脑做服务器)
我在我的电脑上存了一组数据,想把这组数据发送到另外一台电脑(这台电脑做服务器)。要求用Tcp/Ip实现,可以在我的电脑上建立TcpClient,在另外一台电脑上建立Tcp...
我在我的电脑上存了一组数据,想把这组数据发送到另外一台电脑(这台电脑做服务器)。要求用Tcp/Ip实现,可以在我的电脑上建立TcpClient,在另外一台电脑上建立TcpListener实现吗,还有没更好的实现方案。我要用.net,c#语言实现。
展开
2个回答
展开全部
控制台应用1(服务器):
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;using System.Threading;
namespace ConsoleApplication1{ class Program { //服务器端
private static byte[] result = new byte[1024]; private static int myPort = 8081; static Socket serverSocket; static void Main(string[] args) { IPAddress ip = IPAddress.Parse("127.0.0.1"); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(new IPEndPoint(ip, myPort)); serverSocket.Listen(10); Console.WriteLine("启动监听成功!终结点:"+serverSocket.LocalEndPoint); Thread td1 = new Thread(ListenClient); td1.Start(); Console.ReadKey(); serverSocket.Close(); } /// <summary> /// 监听客户端连接 /// </summary> static void ListenClient() { while (true) { Socket clientSocket = serverSocket.Accept(); clientSocket.Send(Encoding.ASCII.GetBytes("come on baby!")); Thread td2 = new Thread(ReceiveMessage); td2.Start(clientSocket); } }
static void ReceiveMessage(object clientSocket) { Socket mySocket = (Socket)clientSocket; while (true) { try { int receiveNum = mySocket.Receive(result); Console.WriteLine("接收到客户端({0})消息:{1}", mySocket.RemoteEndPoint, Encoding.ASCII.GetString(result, 0, receiveNum));
} catch(Exception ex) { Console.WriteLine(ex.Message); mySocket.Shutdown(SocketShutdown.Both); mySocket.Close(); break; } } } }}
控制台应用2(客户端):
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Net;using System.Net.Sockets;
namespace ConsoleApplication2{ class Program { //客户端 static byte[] result = new byte[1024]; static void Main(string[] args) { IPAddress ip = IPAddress.Parse("127.0.0.1"); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { clientSocket.Connect(ip, 8081); Console.WriteLine("连接服务器成功!"); } catch { Console.WriteLine("连接服务器失败!任意键退出..."); Console.ReadKey(); return; } int receiveNum = clientSocket.Receive(result); Console.WriteLine("接收到服务器消息:"+Encoding.ASCII.GetString(result,0,receiveNum));
while (true) { try { Console.Write("你想对服务器说什么('0'键退出):"); string message = Console.ReadLine(); if (!message.Equals("0")) { clientSocket.Send(Encoding.ASCII.GetBytes(message)); Console.WriteLine("发送成功!"); } else { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); break; } } catch { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); break; } } Console.WriteLine("悄悄话已结束。任意键退出..."); Console.ReadKey(); } }}
写的一个小例子,用socket实现的,楼主参考下吧。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;using System.Threading;
namespace ConsoleApplication1{ class Program { //服务器端
private static byte[] result = new byte[1024]; private static int myPort = 8081; static Socket serverSocket; static void Main(string[] args) { IPAddress ip = IPAddress.Parse("127.0.0.1"); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(new IPEndPoint(ip, myPort)); serverSocket.Listen(10); Console.WriteLine("启动监听成功!终结点:"+serverSocket.LocalEndPoint); Thread td1 = new Thread(ListenClient); td1.Start(); Console.ReadKey(); serverSocket.Close(); } /// <summary> /// 监听客户端连接 /// </summary> static void ListenClient() { while (true) { Socket clientSocket = serverSocket.Accept(); clientSocket.Send(Encoding.ASCII.GetBytes("come on baby!")); Thread td2 = new Thread(ReceiveMessage); td2.Start(clientSocket); } }
static void ReceiveMessage(object clientSocket) { Socket mySocket = (Socket)clientSocket; while (true) { try { int receiveNum = mySocket.Receive(result); Console.WriteLine("接收到客户端({0})消息:{1}", mySocket.RemoteEndPoint, Encoding.ASCII.GetString(result, 0, receiveNum));
} catch(Exception ex) { Console.WriteLine(ex.Message); mySocket.Shutdown(SocketShutdown.Both); mySocket.Close(); break; } } } }}
控制台应用2(客户端):
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Net;using System.Net.Sockets;
namespace ConsoleApplication2{ class Program { //客户端 static byte[] result = new byte[1024]; static void Main(string[] args) { IPAddress ip = IPAddress.Parse("127.0.0.1"); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { clientSocket.Connect(ip, 8081); Console.WriteLine("连接服务器成功!"); } catch { Console.WriteLine("连接服务器失败!任意键退出..."); Console.ReadKey(); return; } int receiveNum = clientSocket.Receive(result); Console.WriteLine("接收到服务器消息:"+Encoding.ASCII.GetString(result,0,receiveNum));
while (true) { try { Console.Write("你想对服务器说什么('0'键退出):"); string message = Console.ReadLine(); if (!message.Equals("0")) { clientSocket.Send(Encoding.ASCII.GetBytes(message)); Console.WriteLine("发送成功!"); } else { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); break; } } catch { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); break; } } Console.WriteLine("悄悄话已结束。任意键退出..."); Console.ReadKey(); } }}
写的一个小例子,用socket实现的,楼主参考下吧。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你会不会用mysql之类的ODBC来处理呢?客户端服务器端的程序,你搜一下C# mysql的例子代码吧,
希望对你有帮助
希望对你有帮助
追问
直接连另外一台电脑的数据库存入数据好,还是用tcp/ip将数据发到另外一台电脑,再处理之后存入数据库,这两种方案哪个好。
追答
直接数据库也是底层通过tcp/ip来实现的,
只不过是已经有设计好用的接口给你了,
不需要你自己来封装api,
自己封装非常麻烦的,
还要涉及到拆分字段,很复杂,
希望你使用别人提供好的接口,
方便快捷达到你的目的,
希望对你有帮助
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询