C# socket 指定ip与端口

LIMINLY520 2011-09-10 10:17:36
在使用Socket 中 客户端已经与服务端建立了连接,知道了客户端的ip与端口号
我现在想利用客户端的ip与端口号
发送信息
这个可以实现吗
各位大虾们
请帮我看一下啊
...全文
932 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
LIMINLY520 2011-09-13
  • 打赏
  • 举报
回复
还有人在不??
可否帮我一下的啊
呜呜
cena_jin 2011-09-13
  • 打赏
  • 举报
回复
来学习···
LIMINLY520 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sheln 的回复:]
引用 4 楼 liminly520 的回复:
有可能是我没有说清楚的啊
我是想这样的
就是A为服务器,B,C为客户端
B,C都连接上了服务器,这时我想把B发过来的信息发送给C
这样可以实现吗???

不知道是不是我没理解你的意思。你直接接到B的信息,获取内容转发给C就好了呀。。。。
接收B发来的信息你会吧,发送信息你也会的,那么直接把接收的信息发送出去就可以了额。当然建立了两个Cl……
[/Quote]
呵呵
我就是在建立Client那边有点疑问的啊
不知道该怎样建立的
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liminly520 的回复:]
有可能是我没有说清楚的啊
我是想这样的
就是A为服务器,B,C为客户端
B,C都连接上了服务器,这时我想把B发过来的信息发送给C
这样可以实现吗???
[/Quote]
不知道是不是我没理解你的意思。你直接接到B的信息,获取内容转发给C就好了呀。。。。
接收B发来的信息你会吧,发送信息你也会的,那么直接把接收的信息发送出去就可以了额。当然建立了两个Client,你发要找到谁是ClientC,别发错了。。。去做做你就知道了额
LIMINLY520 2011-09-13
  • 打赏
  • 举报
回复
有可能是我没有说清楚的啊
我是想这样的
就是A为服务器,B,C为客户端
B,C都连接上了服务器,这时我想把B发过来的信息发送给C
这样可以实现吗???
chichenzhe 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liminly520 的回复:]
有可能是我没有说清楚的啊
我是想这样的
就是A为服务器,B,C为客户端
B,C都连接上了服务器,这时我想把B发过来的信息发送给C
这样可以实现吗???
[/Quote]

当然可以实现,否则别人网络游戏里面聊天怎么办.

b,c和a 首先需要建立socket连接. 不要随意close. 也就是长连接模式
b发出的消息 在消息体里面写好 接收者
a根据b的消息的接收者名称 获得c的连接对象. 然后再对c的连接对象发出这条来自b的消息.

相关逻辑组织就是3面3句话.但是具体代码很多,我没有现成的.

这个难度偏大.如果你对socket掌握不够熟练的话 就问问熟的人吧.
再要么搜索一下 C#写的类似 即时通讯类 程序
萧炎 2011-09-10
  • 打赏
  • 举报
回复

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

namespace Server
{
/// <summary>
/// 与客户端的 连接通信类(包含了一个 与客户端 通信的 套接字,和线程)
/// </summary>
public class ConnectionClient
{
Socket sokMsg;
DGShowMsg dgShowMsg;//负责 向主窗体文本框显示消息的方法委托
DGShowMsg dgRemoveConnection;// 负责 从主窗体 中移除 当前连接
Thread threadMsg;

#region 构造函数
/// <summary>
///
/// </summary>
/// <param name="sokMsg">通信套接字</param>
/// <param name="dgShowMsg">向主窗体文本框显示消息的方法委托</param>
public ConnectionClient(Socket sokMsg, DGShowMsg dgShowMsg, DGShowMsg dgRemoveConnection)
{
this.sokMsg = sokMsg;
this.dgShowMsg = dgShowMsg;
this.dgRemoveConnection = dgRemoveConnection;

this.threadMsg = new Thread(RecMsg);
this.threadMsg.IsBackground = true;
this.threadMsg.Start();
}
#endregion

bool isRec = true;
#region 02负责监听客户端发送来的消息
void RecMsg()
{
while (isRec)
{
try
{
byte[] arrMsg = new byte[1024 * 1024 * 2];
//接收 对应 客户端发来的消息
int length = sokMsg.Receive(arrMsg);
//将接收到的消息数组里真实消息转成字符串
string strMsg = System.Text.Encoding.UTF8.GetString(arrMsg, 0, length);
//通过委托 显示消息到 窗体的文本框
dgShowMsg(strMsg);
}
catch (Exception ex)
{
isRec = false;
//从主窗体中 移除 下拉框中对应的客户端选择项,同时 移除 集合中对应的 ConnectionClient对象
dgRemoveConnection(sokMsg.RemoteEndPoint.ToString());
}
}
}
#endregion

#region 03向客户端发送消息
/// <summary>
/// 向客户端发送消息
/// </summary>
/// <param name="strMsg"></param>
public void Send(string strMsg)
{
byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);
byte[] arrMsgFinal = new byte[arrMsg.Length+1];

arrMsgFinal[0] = 0;//设置 数据标识位等于0,代表 发送的是 文字
arrMsg.CopyTo(arrMsgFinal, 1);

sokMsg.Send(arrMsgFinal);
}
#endregion

#region 04向客户端发送文件数据 +void SendFile(string strPath)
/// <summary>
/// 04向客户端发送文件数据
/// </summary>
/// <param name="strPath">文件路径</param>
public void SendFile(string strPath)
{
//通过文件流 读取文件内容
using (FileStream fs = new FileStream(strPath, FileMode.OpenOrCreate))
{
byte[] arrFile = new byte[1024 * 1024 * 2];
//读取文件内容到字节数组,并 获得 实际文件大小
int length = fs.Read(arrFile, 0, arrFile.Length);
//定义一个 新数组,长度为文件实际长度 +1
byte[] arrFileFina = new byte[length + 1];
arrFileFina[0] = 1;//设置 数据标识位等于1,代表 发送的是文件
//将 文件数据数组 复制到 新数组中,下标从1开始
//arrFile.CopyTo(arrFileFina, 1);
Buffer.BlockCopy(arrFile, 0, arrFileFina, 1, length);
//发送文件数据
sokMsg.Send(arrFileFina);//, 0, length + 1, SocketFlags.None);
}
}
#endregion

#region 05向客户端发送闪屏
/// <summary>
/// 向客户端发送闪屏
/// </summary>
/// <param name="strMsg"></param>
public void SendShake()
{
byte[] arrMsgFinal = new byte[1];
arrMsgFinal[0] = 2;
sokMsg.Send(arrMsgFinal);
}
#endregion

#region 06关闭与客户端连接
/// <summary>
/// 关闭与客户端连接
/// </summary>
public void CloseConnection()
{
isRec = false;
}
#endregion
}
}
Lemon2050 2011-09-10
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;

namespace tcpserver
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class server
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
int recv;//用于表示客户端发送的信息长度
byte[] data = new byte[1024];//用于缓存客户端所发送的信息,通过socket传递的信息必须为字节数组
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);//本机预使用的IP和端口
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);//绑定
newsock.Listen(10);//监听
Console.WriteLine("waiting for a client");
Socket client = newsock.Accept();//当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信
IPEndPoint clientip = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("connect with client:" + clientip.Address + " at port:" + clientip.Port);
string welcome = "welcome here!";
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length, SocketFlags.None);//发送信息
while (true)
{//用死循环来不断的从客户端获取信息
data = new byte[1024];
recv = client.Receive(data);
Console.WriteLine("recv=" + recv);
if (recv == 0)//当信息长度为0,说明客户端连接断开
break;
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
client.Send(data, recv, SocketFlags.None);
}
Console.WriteLine("Disconnected from" + clientip.Address);
client.Close();
newsock.Close();

}
}
}





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


namespace tcpclient
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class client
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
byte[] data = new byte[1024];
Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.Write("please input the server ip:");
string ipadd = Console.ReadLine();
Console.WriteLine();
Console.Write("please input the server port:");
int port = Convert.ToInt32(Console.ReadLine());
IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port);//服务器的IP和端口
try
{
//因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。
newclient.Connect(ie);
}
catch (SocketException e)
{
Console.WriteLine("unable to connect to server");
Console.WriteLine(e.ToString());
return;
}
int recv = newclient.Receive(data);
string stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
while (true)
{
string input = Console.ReadLine();
if (input == "exit")
break;
newclient.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = newclient.Receive(data);
stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
}
Console.WriteLine("disconnect from sercer");
newclient.Shutdown(SocketShutdown.Both);
newclient.Close();

}
}
}



110,536

社区成员

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

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

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