在线等,关于C# TCP/IP Socket 的 输据传输问题!!!!!!!!
大家好 ,本人现在在做一个家庭安全系统,用TCP/IP Socket 写了 Client 和 server 两个终端。房子的模型(装有几个感应器),当感应器一触发,那么server 端会发出 数据,client 会接受 并作出相应的判断(播放警报音乐文件并发送email)。有些基本的都没有问题了,现在就是当我实际测试的时候,当我的 client接收到server发送来得数据,并要播放音乐文件和发送email时不能正常工作。
以下列出:
1:音乐文件播放不完全(断断续续的,并每次都是从头开始,之间间隙只有零点几秒),我使用API函数播放wav文件,最初使用简单的按钮测试没有问题。
--------------------------------------------------------------------音乐播放的code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace smarthome
{
class sound
{
[Flags]
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0000, /* play synchronously (default) */ //同步
SND_ASYNC = 0x0001, /* play asynchronously */ //异步
SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */
SND_MEMORY = 0x0004, /* pszSound points to a memory file */
SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS = 0x00010000, /* name is a registry alias */
SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
SND_FILENAME = 0x00020000, /* name is file name */
SND_RESOURCE = 0x00040004 /* name is resource name or atom */
}
[DllImport("winmm.dll")]
public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );
}
public class Sound
{
//播放
public static void Play( string strFileName )
{
switch(strFileName)
{
case "error": strFileName = @"C:\Documents and Settings\Qiu Wei\My Documents\Visual Studio 2005\Projects\smarthome\smarthome\Resources\1.wav"; break;
case "warning": strFileName = @"C:\Documents and Settings\Qiu Wei\My Documents\Visual Studio 2005\Projects\smarthome\smarthome\Resources\2.wav"; break;
/* case "fall": strFileName=@"..\..\sound\fall.WAV"; break;
case "huiqi": strFileName=@"..\..\sound\huiqi.WAV"; break;
case "huiqiend": strFileName=@"..\..\sound\huiqiend.WAV"; break;
case "jiangjun": strFileName=@"..\..\sound\jiangjun.WAV"; break;
case "kill": strFileName=@"..\..\sound\kill.WAV"; break;
case "win": strFileName=@"..\..\sound\win.WAV"; break;
case "move": strFileName=@".\start.WAV"; break;
case "hold": strFileName=@".\stop.WAV"; break;
case "no": strFileName=@"..\..\sound\no.WAV"; break;
case "popup": strFileName=@"..\..\sound\popup.WAV"; break;
case "mayfall": strFileName=@"..\..\sound\mayfall.WAV"; break; */
}
//调用PlaySound方法,播放音乐
sound.PlaySound(strFileName, IntPtr.Zero, sound.PlaySoundFlags.SND_ASYNC);
}
//关闭
public static void Stop()
{
sound.PlaySound(null, IntPtr.Zero, sound.PlaySoundFlags.SND_ASYNC);
}
}
}
--------------------------------------------------------------------
接收数据部分的code:
public void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
public void OnDataReceived(IAsyncResult asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
//end receive...
int iRx = 0;
iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
txtDataRx.Text = szData;
if (txtDataRx.Text == "CBH")
{
sensortextBox1.Text = "Door Close";
sensortextBox2.Text = "Door Block";
timer1.Start();
sound();
pictureBox9.Visible = true;
pictureBox11.Visible = false;
pictureBox10.Visible = false;
pictureBox12.Visible = true;
}
if (txtDataRx.Text == "OBH")
{
sensortextBox1.Text = "Door Open";
sensortextBox2.Text = "Door Block";
timer1.Start();
sound();
pictureBox9.Visible = false;
pictureBox11.Visible = true;
pictureBox10.Visible = false;
pictureBox12.Visible = true;
}
}
--------------------------------------------------------------------
2:有个关于发送email的问题,我在家测试 的时候能够通过c#发送email,但是如果用了代理服务器,系统会显示无法与远端短信服务器连接。请问问题关键是在于代理?
问题比较棘手!!!希望有经验的朋友能够给一些意见和建议!谢谢!
[解决办法]
可以能是防火墙的问题
你用什么方法发EMAIL啊
------解决方案--------------------
Socket。。。
有代理的话就需要设置了,参考
public class WebRequestThroughProxy { public static void Main() { // Create a request for the URL. WebRequest request = WebRequest.Create("http://www.baidu.com"); // If required by the server, set the credentials. ProxySetting(request); request.Credentials = CredentialCache.DefaultCredentials; // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Display the status. Console.WriteLine(response.StatusDescription); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. Console.WriteLine(responseFromServer); // Cleanup the streams and the response. reader.Close(); dataStream.Close(); response.Close(); } public static void ProxySetting(WebRequest request) { WebProxy proxy = WebProxy.GetDefaultProxy();//获取IE缺省设置 //如果缺省设置为空,则有可能是根本不需要代理服务器,如果此时配置文件中也未配置则认为不需Proxy if (proxy.Address == null && ProxyAddress != null && ProxyAddress != "") proxy.Address = new Uri(ProxyAddress);//按配置文件创建Proxy 地置 if (proxy.Address != null)//如果地址为空,则不需要代理服务器 { proxy.Credentials = new NetworkCredential(ProxyUser, ProxyKey);//从配置封装参数中创建 request.Proxy = proxy;//赋予 request.Proxy } } public const string ProxyAddress = ""; public const string ProxyUser = "user"; public const string ProxyKey = "password"; }
[解决办法]
代理是否与socket收发数据有关我也不知道,你可以试试上面的方法
[解决办法]
觉得程序的逻辑问题
按钮和实际的区别在于 触发的次数和频率是你自己来指定的
实际的C、S模式下 可能由于某个问题 多次重复的触发和调用了
可以找找原因
同时可以设计一个开发 使在一个音乐没有播放完全的时间内 不再响应请求