菜鸟问题:socket接收值总是“\0”
--------------------编程问答-------------------- 不懂,顶个.. --------------------编程问答-------------------- using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
namespace socketConn
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
Thread SocketThrd = new Thread(socketStart);
SocketThrd.IsBackground = true;
SocketThrd.Start();
}
private byte[] data = new byte[1024];
private Socket newsock;
private Socket newclient;
private void socketStart()
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(5);
while (true)
{
newclient = newsock.Accept();
Thread newThread = new Thread(thrdFunc);
newThread.Start();
}
}
private void thrdFunc()
{
int recv, recv1;
recv = newclient.Receive(data);
userName = Encoding.ASCII.GetString(data, 0, recv);
while (true)
{
try
{
data = new byte[64];
recv1 = newclient.Receive(data);
switch (recv1)
{
case 2:
{
string msg = Encoding.ASCII.GetString(data, 0, recv1);
if (msg == "10")
MessageBox.Show("OK");
else
MessageBox.Show("Fail");
break;
}
default:
break;
}
}
catch (SocketException)
{
MessageBox.Show(userName + "'s connection is abnormal\n\n At: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm"), ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
Thread.CurrentThread.Abort();
}
}
}
}
} --------------------编程问答-------------------- 我把要实现的业务功能去掉,简洁点,大家帮帮忙啊!
补充:.NET技术 , C#