本文介绍了如何从Web应用程序连接到Windows应用程序并以unicode格式发送数据并在sserver套接字中接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using(TcpClient client = new TcpClient(ConfigurationManager.AppSettings [TcpIp],2000))//192.168.3.82,4973 5000

using(NetworkStream networkStream = client.GetStream( ))

{

byte [] usernameBytes = Encoding.ASCII.GetBytes(text);

networkStream.Write(usernameBytes,0,usernameBytes .GetLength(0));

networkStream.Close();

}



这是我的代码在Web应用程序即时发送unicode但事情是我没有连接到套接字在正常运行它将连接在调试模式PLZ帮我这个代码asap



我尝试过:



public void OnClientConnect(IAsyncResult asyn)

{

try

{

//这里我们完成/结束BeginAccept()异步调用

/ /通过调用EndAccept() - 返回引用

//一个新的Socket对象

Socket workerSocket = m_mainSocket.EndAccept(asyn);



//现在以线程安全的方式增加此客户端的客户端数量

//

Interlocked.Increment(ref m_clientCount) ;



//将workerSocket引用添加到我们的ArrayList

GP.m_workerSocketList.Add(workerSocket);



//向客户发送欢迎信息

string msg =欢迎客户+ m_clientCount +\ n;

SendMsgToClient (msg,m_clientCount);



// OnDataReceived(asyn);



//更新列表显示客户列表的框(线程安全调用)

UpdateClientListC ontrol();



//让工人Socket为

//连接的客户进行进一步处理

WaitForData(workerSocket,m_clientCount);



//由于主Socket现在是免费的,它可以返回等待

//正在尝试连接的其他客户端

m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect),null);



}

catch(ObjectDisposedException)

{

System.Diagnostics.Debugger.Log(0,1,\ n OnClientConnection:Socket已被已关闭\ n;

}

catch(SocketException se)

{

MessageBox.Show( se.Message);

}

catch(例外)

{

MessageBox.Show(在TCPServer中的er.Message +\ n OnClientConnect函数);

}

}



以上代码在Windows应用程序中

using (TcpClient client = new TcpClient(ConfigurationManager.AppSettings["TcpIp"], 2000)) //"192.168.3.82", 4973 5000
using (NetworkStream networkStream = client.GetStream())
{
byte[] usernameBytes = Encoding.ASCII.GetBytes(text);
networkStream.Write(usernameBytes, 0, usernameBytes.GetLength(0));
networkStream.Close();
}

This is my code in web application im sending the unicode but the thing is that im not connected to the socket in normal running it will connect in debug mode plz help me for this code asap

What I have tried:

public void OnClientConnect(IAsyncResult asyn)
{
try
{
// Here we complete/end the BeginAccept() asynchronous call
// by calling EndAccept() - which returns the reference to
// a new Socket object
Socket workerSocket = m_mainSocket.EndAccept(asyn);

// Now increment the client count for this client
// in a thread safe manner
Interlocked.Increment(ref m_clientCount);

// Add the workerSocket reference to our ArrayList
GP.m_workerSocketList.Add(workerSocket);

// Send a welcome message to client
string msg = "Welcome client " + m_clientCount + "\n";
SendMsgToClient(msg, m_clientCount);

// OnDataReceived(asyn);

// Update the list box showing the list of clients (thread safe call)
UpdateClientListControl();

// Let the worker Socket do the further processing for the
// just connected client
WaitForData(workerSocket, m_clientCount);

// Since the main Socket is now free, it can go back and wait for
// other clients who are attempting to connect
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);

}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
catch (Exception er)
{
MessageBox.Show(er.Message + "\n OnClientConnect function inside TCPServer");
}
}

the above code is in windows application

推荐答案


这篇关于如何从Web应用程序连接到Windows应用程序并以unicode格式发送数据并在sserver套接字中接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 11:13