谁能给一个C# C/S的实例,必须要用SOCKET的

hhc123 2009-11-01 09:54:07
如上
...全文
205 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhc123 2009-11-05
  • 打赏
  • 举报
回复
我不想要FTP,聊天系列的,这些我多得是
lizhigang770 2009-11-05
  • 打赏
  • 举报
回复
http://win.51aspx.com/CV/CsharpFtp/

这个是C# FTP 源代码
lizhigang770 2009-11-05
  • 打赏
  • 举报
回复
FlyFish网络下载工具
http://win.51aspx.com/CV/FlyFish/

这个可以了吧?? 给分
hhc123 2009-11-05
  • 打赏
  • 举报
回复


要进销存之类的实例,如FTP,聊天系列的通通不要,必定要用SOcket的,不可以用emoting或WCF,
高手来,分少我会再加的

hhc123 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lizhigang770 的回复:]
一个完整的例子,client向server发送一段测试字符串,server接收并显示出来,给予client成功响应。

C# code//client.csusing System;using System.Collections.Generic;using System.Text;using System.IO;using System.Net;using System.Net.Sockets;namespace client
{class class1

{staticvoid Main(string[] args)
{try
{int port=2000;string host="127.0.0.1";
IPAddress ip= IPAddress.Parse(host);
IPEndPoint ipe=new IPEndPoint(ip,port);//把ip和端口转化为IPEndPoint实例 Socket c=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//创建一个Socket Console.WriteLine("Conneting...");
c.Connect(ipe);//连接到服务器string sendStr="hello!This is a socket test";byte[] bs=Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine("SendMessage");
c.Send(bs,bs.Length,0);//发送测试信息string recvStr="";byte[]recvBytes= newbyte[1024];int bytes;
bytes= c.Receive(recvBytes,recvBytes.Length,0);//从服务器端接受返回信息 recvStr+= Encoding.ASCII.GetString(recvBytes,0,bytes);
Console.WriteLine("ClientGetMessage:{0}",recvStr);//显示服务器返回信息 c.Close();
}catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException:{0}",e);
}catch(SocketException e)
{
Console.WriteLine("SocketException:{0}",e);
}
Console.WriteLine("PressEntertoExit");
Console.ReadLine();
}
}
}//server.cs//server端using System;using System.Text;using System.IO;using System.Net;using System.Net.Sockets;namespace sever{
 class Class2
  {
  staticvoid Main()
   {
   try
    {
    int port=2000;
    string host="127.0.0.1";
     IPAddress ip= IPAddress.Parse(host);
     IPEndPoint ipe=new IPEndPoint(ip,port);
     Socket s=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//创建一个Socket类     s.Bind(ipe);//绑定2000端口     s.Listen(0);//开始监听     Console.WriteLine("Wait for connect");
     Socket temp= s.Accept();//为新建连接创建新的Socket。     Console.WriteLine("Get a connect");
    string recvStr="";
    byte[] recvBytes=newbyte[1024];
    int bytes;
     bytes= temp.Receive(recvBytes,recvBytes.Length,0);//从客户端接受信息     recvStr+= Encoding.ASCII.GetString(recvBytes,0,bytes);
     Console.WriteLine("ServerGetMessage:{0}",recvStr);//把客户端传来的信息显示出来    string sendStr="Ok! Client Send Message Sucessful!";
    byte[] bs= Encoding.ASCII.GetBytes(sendStr);
     temp.Send(bs,bs.Length,0);//返回客户端成功信息     temp.Close();
     s.Close();
    }
   catch(ArgumentNullException e)
    {
     Console.WriteLine("ArgumentNullException:{0}",e);
    }
   catch(SocketException e)
    {
     Console.WriteLine("SocketException:{0}",e);
    }
    Console.WriteLine("Press Enter to Exit");
    Console.ReadLine();
   }
  }
}
[/Quote]
请认真看题,谢谢
lizhigang770 2009-11-05
  • 打赏
  • 举报
回复
一个完整的例子,client向server发送一段测试字符串,server接收并显示出来,给予client成功响应。


//client.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace client
{
class class1

{
static void Main(string[] args)
{
try
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip,port);//把ip和端口转化为IPEndPoint实例
Socket c = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//创建一个Socket
Console.WriteLine("Conneting...");
c.Connect(ipe);//连接到服务器
string sendStr="hello!This is a socket test";
byte[] bs=Encoding.ASCII.GetBytes(sendStr);
Console.WriteLine("SendMessage");
c.Send(bs,bs.Length,0);//发送测试信息
string recvStr="";
byte[]recvBytes = newbyte[1024];
int bytes;
bytes = c.Receive(recvBytes,recvBytes.Length,0);//从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes,0,bytes);
Console.WriteLine("ClientGetMessage:{0}",recvStr);//显示服务器返回信息
c.Close();
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException:{0}",e);
}
catch(SocketException e)
{
Console.WriteLine("SocketException:{0}",e);
}
Console.WriteLine("PressEntertoExit");
Console.ReadLine();
}
}
}



//server.cs

//server端
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace sever{
  class Class2
  {
   static void Main()
   {
    try
    {
     int port = 2000;
     string host = "127.0.0.1";
     IPAddress ip = IPAddress.Parse(host);
     IPEndPoint ipe = new IPEndPoint(ip,port);
     Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//创建一个Socket类
     s.Bind(ipe);//绑定2000端口
     s.Listen(0);//开始监听
     Console.WriteLine("Wait for connect");
     Socket temp = s.Accept();//为新建连接创建新的Socket。
     Console.WriteLine("Get a connect");
     string recvStr = "";
     byte[] recvBytes = new byte[1024];
     int bytes;
     bytes = temp.Receive(recvBytes,recvBytes.Length,0);//从客户端接受信息
     recvStr += Encoding.ASCII.GetString(recvBytes,0,bytes);
     Console.WriteLine("ServerGetMessage:{0}",recvStr);//把客户端传来的信息显示出来
     string sendStr = "Ok! Client Send Message Sucessful!";
     byte[] bs = Encoding.ASCII.GetBytes(sendStr);
     temp.Send(bs,bs.Length,0);//返回客户端成功信息
     temp.Close();
     s.Close();
    }
    catch(ArgumentNullException e)
    {
     Console.WriteLine("ArgumentNullException:{0}",e);
    }
    catch(SocketException e)
    {
     Console.WriteLine("SocketException:{0}",e);
    }
    Console.WriteLine("Press Enter to Exit");
    Console.ReadLine();
   }
  }
}

ermuzi 2009-11-05
  • 打赏
  • 举报
回复
好难找哦,慢慢找哈
......
hhc123 2009-11-05
  • 打赏
  • 举报
回复
再顶
hhc123 2009-11-05
  • 打赏
  • 举报
回复
我顶
ryukenchyan 2009-11-02
  • 打赏
  • 举报
回复
那这个你用Sokcet很麻烦也。建议使用Remoting或WCF
hhc123 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ryukenchyan 的回复:]
那这个你用Sokcet很麻烦也。建议使用Remoting或WCF
[/Quote]
就是麻烦不是一两句可以说明白,才要个实例看看
hhc123 2009-11-01
  • 打赏
  • 举报
回复
要进销存之类的实例,如FTP,聊天系列的通通不要
谢谢

110,556

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧