こんにちは私のサーバーは、TcpClient.Writeから間違った値を読み取ると思うが、それは52(私はそれを送信する瞬間にはバイト配列)が、私はTcpClient.Readでのサイズを見ているときにサーバー側でallways 8192です。間違ってる?TcpClient.ReadがTcpClient.Writeから間違った値を読み取る?
私のクライアント:あなたは実際のバイト数についての情報を捨てる読み、代わりにバッファサイズを使用しているためだ
using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;
namespace client
{
public partial class Form1 : Form
{
TcpClient clientSocket = new TcpClient();
NetworkStream serverStream;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Multiline = true;
msg("Client Started");
label1.Text = "Client Socket Program - ";
}
private void button1_Click(object sender, EventArgs e)
{
Send();
}
public void msg(string mesg)
{
textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
}
private void button2_Click(object sender, EventArgs e)
{
clientSocket.Connect("192.168.1.109", 8888);
label1.Text = "Client Socket Program - Server Connected ...";
}
private void Send()
{
NetworkStream serverStream = clientSocket.GetStream();
XElement outXML =XElement.Load("test.xml") ;
byte[] outStream = ConvertXmlToByteArray(outXML, Encoding.UTF8);
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
}
byte[] ConvertXmlToByteArray(XElement xml, Encoding encoding)
{
using (MemoryStream stream = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = encoding;
settings.OmitXmlDeclaration = true; // No prolog
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
xml.Save(writer);
}
return stream.ToArray();
}
}
}
}
マイサーバー
using System;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = new TcpClient();
int counter = 0;
serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");
counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
handleClinet client = new handleClinet();
client.startClient(clientSocket, Convert.ToString(counter));
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
}
//Class to handle each client request separatly
public class handleClinet
{
TcpClient clientSocket;
string clNo;
public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
requestCount = 0;
while ((true))
{
byte[] bytesFrom = new byte[(int)clientSocket.ReceiveBufferSize];
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
Console.Write(clientSocket.ReceiveBufferSize.ToString());
XElement inXML = ConvertByteArrayToXml(bytesFrom);
inXML.Save("jest2.xml");
//XMLSave(dataFromClient);
}
}
//private void XMLSave(string inner)
//{
// XmlDocument XmlDoc = new XmlDocument();
// XmlDoc.Load("jest.xml");
// XmlDeclaration xmlDeclaration = XmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
// XmlNode nextNode = XmlDoc.CreateElement("root");
// nextNode.InnerText = inner;
// XmlDoc.DocumentElement.AppendChild(nextNode);
// XmlDoc.Save("jest.xml");
//}
XElement ConvertByteArrayToXml(byte[] data)
{
XmlReaderSettings settings = new XmlReaderSettings();
using (MemoryStream stream = new MemoryStream(data))
using (XmlReader reader = XmlReader.Create(stream, settings))
{
return XElement.Load(reader);
}
}
private byte[] RemoveNulls(byte[] DataStream)
{
for (int i = 0; i < DataStream.Length - 1; i++)
{
if (DataStream[i] == 0x00) DataStream[i] = 0x01;
}
return DataStream;
}
}
}
が私が見るメッセージの終わりに区切りを使用しているバイトの数、どうもありがとうを含むヘッダーを導入:) –