[请进]       用C#发送数据包,给一个实例马上结分

RexZheng 2005-12-29 05:47:30
怎么指定一个本地端口来连接服务器,并向服务器发送数据包!
...全文
308 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
心情解码 2005-12-30
  • 打赏
  • 举报
回复
:)
wangsaokui 2005-12-30
  • 打赏
  • 举报
回复
[C#]
static void Connect(String server, String message)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(server, port);

// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

// Get a client stream for reading and writing.
// Stream stream = client.GetStream();

NetworkStream stream = client.GetStream();

// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);

Console.WriteLine("Sent: {0}", message);

// Receive the TcpServer.response.

// Buffer to store the response bytes.
data = new Byte[256];

// String to store the response ASCII representation.
String responseData = String.Empty;

// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);

// Close everything.
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}

Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
liujx_1999 2005-12-30
  • 打赏
  • 举报
回复
dazhu2(边城浪子【倚天不出,谁与争锋】) :
也给我一份吧。兄弟
liujx_1999@yahoo.com.cn
稍微有点无敌 2005-12-30
  • 打赏
  • 举报
回复
dazhu2(边城浪子【倚天不出,谁与争锋】) :
也给我一份好吗
jrt324@126.com
RexZheng 2005-12-29
  • 打赏
  • 举报
回复
谢谢收到了,正在看

再问一下,怎么指定本地端口?
dazhu2 2005-12-29
  • 打赏
  • 举报
回复
刚发了,我把客户端和服务器端都封装成类了,还有部分东西还没有写好,是“停止发送或接受”
RexZheng 2005-12-29
  • 打赏
  • 举报
回复
谢谢dazhu2(边城浪子【倚天不出,谁与争锋】)
v0009@163.com
dazhu2 2005-12-29
  • 打赏
  • 举报
回复
我给一个发送文件的例子给你,你给出邮箱
RexZheng 2005-12-29
  • 打赏
  • 举报
回复
高手都那去了?

帮帮忙
RexZheng 2005-12-29
  • 打赏
  • 举报
回复
谢谢楼上的,我知道可以用tcpclient,但是在指定本地端口这里遇到困难了.

详细点说可吗?最好能给一个实例
hyj_828 2005-12-29
  • 打赏
  • 举报
回复
tcp/udp
socket
都可以實現.

110,537

社区成员

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

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

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