c# 怎样发送一段字符串给服务器? c# 怎样发送一段字符串给服务器?知道服务器ip 192.168.10.23 端口 8777 发送字段“1,2,3” 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Net.Sockets;using System.IO;using System.Threading ; namespace P2PSvr{ /// <summary> /// clsP2P的摘要说明。 /// </summary> public class P2P { public delegate void DelgMsgListened(); public event DelgMsgListened OnMsgListened; //侦听到的消息 private string m_msgLisened; //远程机是否正常连接 private bool isConnected=false; //创建线程,用以侦听端口号,接收信息 private Thread lsThread; //用以侦听端口号 private TcpListener lsClient; //设定标识位,判断侦听状态 private bool isListenerNow = true; //创建传送/接收的基本数据流实例 private NetworkStream tcpStream; //用以实现向远程主机传送信息 private StreamWriter reqStreamW; //用以创建对远程主机的连接 private TcpClient tcpClientPere; //用以接收远程主机传送来的数据 private Socket skSocket; public string MsgLisened { get { return m_msgLisened; } } public bool Connected { get { return isConnected; } } public void DisposeMe() { try { isListenerNow = false; lsThread.Abort(); lsThread = null; lsClient.Stop (); skSocket.Close(); tcpClientPere.Close(); } catch(Exception) { //Do nothing here } } public void BeginLisen() { try { lsClient= new TcpListener (8777); lsClient.Start(); //接受远程计算机的连接请求,并获得用以接收数据的Socket实例 skSocket = lsClient.AcceptSocket(); //循环侦听 while (isListenerNow) { //定义从远程计算机接收到数据存放的数据缓冲区 Byte[] stream = new Byte[80] ; //接收数据,并存放到定义的缓冲区中 int i = skSocket.Receive(stream); //以指定的编码,从缓冲区中读出内容 m_msgLisened = System.Text.Encoding.UTF8.GetString (stream); isConnected=true; OnMsgListened(); } } catch (System.Security.SecurityException ) { isConnected=false; } } public void ThreadToListen() { //初始化线程实例,单独以一个线程来进行侦听 lsThread = new Thread (new ThreadStart(this.BeginLisen)); //启动线程 lsThread.Start(); } public bool ConnectToRemote(string svrName) { try { tcpClientPere = new TcpClient() ; //向远程计算机提出连接申请 tcpClientPere.Connect(svrName,8777); //如果连接申请建立,获得传送数据的数据流 tcpStream = tcpClientPere.GetStream(); return true; } catch (Exception) { return false; } } public void DisConnectToRemote() { tcpClientPere.Close(); tcpClientPere = null; } public bool SendMsg(string msgToSend) { try { reqStreamW = new StreamWriter(tcpStream); reqStreamW.Write (msgToSend); //清理当前编写器的所有缓冲区,并使所有缓冲数据写入流 reqStreamW.Flush ( ) ; return true; } //异常处理 catch (Exception) { return false; } } }//发送消息public void Send() { if (SendMsg("1,2,3")) { lstMsg.Items.Add("1,2,3") ; txtSend.Clear(); } else { txtSend.Text = "无法发送消息到目标计算机!"; } }} 利用可靠传输TCP/IP进行传输,上面已有代码. 3L怎样实例化指定服务器IP啊? FTP获取文件名是报‘数据连接关闭,传输中止’异常 付上代码了 请问,WPF只是做桌面程序吗? 多线程与方法的调用的一个疑惑 能用gridview实现这个吗? 有没有人针对数字电视做用户管理系统的 有关random的简单问题 请教一下VS的操作的方便性,感谢进来看一看啊,有答案就结贴 winform中如何让弹出窗体为活动窗体 怎样将DataGrid中将已有的日期型字段置空? 我怎么在VS.NET里面找不到AL(程序集连接器)??请指个路径好么? 怎样将datagridview中的一列进行锁定 WCF服务问题
using System.Net.Sockets;
using System.IO;
using System.Threading ; namespace P2PSvr
{
/// <summary>
/// clsP2P的摘要说明。
/// </summary>
public class P2P
{
public delegate void DelgMsgListened();
public event DelgMsgListened OnMsgListened; //侦听到的消息
private string m_msgLisened;
//远程机是否正常连接
private bool isConnected=false; //创建线程,用以侦听端口号,接收信息
private Thread lsThread;
//用以侦听端口号
private TcpListener lsClient;
//设定标识位,判断侦听状态
private bool isListenerNow = true; //创建传送/接收的基本数据流实例
private NetworkStream tcpStream;
//用以实现向远程主机传送信息
private StreamWriter reqStreamW;
//用以创建对远程主机的连接
private TcpClient tcpClientPere;
//用以接收远程主机传送来的数据
private Socket skSocket;
public string MsgLisened
{
get
{
return m_msgLisened;
}
} public bool Connected
{
get
{
return isConnected;
}
} public void DisposeMe()
{
try
{
isListenerNow = false;
lsThread.Abort();
lsThread = null;
lsClient.Stop ();
skSocket.Close();
tcpClientPere.Close();
}
catch(Exception)
{
//Do nothing here
}
} public void BeginLisen()
{
try
{
lsClient= new TcpListener (8777);
lsClient.Start();
//接受远程计算机的连接请求,并获得用以接收数据的Socket实例
skSocket = lsClient.AcceptSocket();
//循环侦听
while (isListenerNow)
{
//定义从远程计算机接收到数据存放的数据缓冲区
Byte[] stream = new Byte[80] ;
//接收数据,并存放到定义的缓冲区中
int i = skSocket.Receive(stream);
//以指定的编码,从缓冲区中读出内容
m_msgLisened = System.Text.Encoding.UTF8.GetString (stream);
isConnected=true;
OnMsgListened();
}
}
catch (System.Security.SecurityException )
{
isConnected=false;
}
} public void ThreadToListen()
{
//初始化线程实例,单独以一个线程来进行侦听
lsThread = new Thread (new ThreadStart(this.BeginLisen));
//启动线程
lsThread.Start();
} public bool ConnectToRemote(string svrName)
{
try
{
tcpClientPere = new TcpClient() ;
//向远程计算机提出连接申请
tcpClientPere.Connect(svrName,8777);
//如果连接申请建立,获得传送数据的数据流
tcpStream = tcpClientPere.GetStream();
return true;
}
catch (Exception)
{
return false;
}
} public void DisConnectToRemote()
{
tcpClientPere.Close();
tcpClientPere = null;
} public bool SendMsg(string msgToSend)
{
try
{
reqStreamW = new StreamWriter(tcpStream);
reqStreamW.Write (msgToSend);
//清理当前编写器的所有缓冲区,并使所有缓冲数据写入流
reqStreamW.Flush ( ) ;
return true;
}
//异常处理
catch (Exception)
{
return false;
}
}
}//发送消息
public void Send()
{ if (SendMsg("1,2,3"))
{
lstMsg.Items.Add("1,2,3") ;
txtSend.Clear();
}
else
{
txtSend.Text = "无法发送消息到目标计算机!";
}
}
}