PDA

View Full Version : socket programing+کمک



silverman_200
شنبه 23 خرداد 1388, 11:36 صبح
سلام دوستان من میخواستم یه متن رو به شکل کلاینت سرور بفرستم کد اونو دارم ولی میخواستم همزمان با ارسال خود متن hash اون رو هم بفرستم و در قسمت سرور اگه hash بست اومده از متن با hash ارسال شده برابر بود اونو نشون بدم کسی میتونه بهم کمک کنه؟
مرسی

اینم کد مربوط به قسمت کلاینت:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Client
{
public partial class Form1 : Form
{
Socket clientSocket = null;
IPEndPoint destination = null;
byte[] sendBuffer, recvBuffer;
Thread readThread;
ProtocolType sockProtocol;
SocketType sockType;
int rc,counter;
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
counter = 0;
}
public static void FormatBuffer(byte[] dataBuffer, string message)
{
byte[] byteMessage = System.Text.Encoding.Unicode.GetBytes(message);
int index = 0;
while (index < byteMessage.Length)
{
for (int j = 0; j < dataBuffer.Length; j++)
{
// Make sure another byte of data buffer became zero
if (index >= byteMessage.Length)
{
dataBuffer[j] = 0;
}
else
{
dataBuffer[j] = byteMessage[index];
index++;
}
}
}
}
private void RunClient()
{
sockType = SocketType.Stream;
sockProtocol = ProtocolType.Tcp;
IPAddress addr = IPAddress.Parse("127.0.0.1");
int remotePort = 1370;
// Size of the send and receive buffers
int bufferSize = 4096;
int rc;
// Specified TCP
sockType = SocketType.Stream;
sockProtocol = ProtocolType.Tcp;
sendBuffer = new byte[bufferSize];
recvBuffer = new Byte[bufferSize];
try
{
clientSocket = new Socket(
addr.AddressFamily,
sockType,
sockProtocol
);
try
{
// Create the endpoint that describes the destination
destination = new IPEndPoint(addr, remotePort);
StatusLabel1.Text = "Attempting connection to: " + destination.ToString();
clientSocket.Connect(destination);

if (clientSocket != null)
StatusLabel1.Text = "Connection to Server on " + destination.ToString();
}
catch (SocketException errr)
{
// Connect faile so close the socket and try the next address
StatusLabel1.Text = errr.Message;
clientSocket.Close();
clientSocket = null;
BtnDC_Click(null, null);
}
// Make sure we have a valid socket before trying to use it
if (clientSocket != null)
{
// Receive data in a loop until the server closes the connection. For
// TCP this occurs when the server performs a shutodwn or closes
// the socket. For UDP, we'll know to exit when the remote host
// sends a zero byte datagram.
while (true)
{
rc = clientSocket.Receive(recvBuffer);

StatusLabel1.Text = "Read " + rc + " bytes";
if (rc > 0)
{
string pss = System.Text.Encoding.Unicode.GetString(recvBuffer) ;

string msg=System.Text.Encoding.Unicode.GetString(recvBuf fer);


textBox1.Text += msg ;
textBox2.Text += pss;


}
// Exit loop if server indicates shutdown
if (rc == 0)
{
clientSocket.Close();
break;
}
}
}
else
{
StatusLabel1.Text = "Unable to establish connection to server!";
BtnDC_Click(null, null);
}
}
catch (SocketException err)
{
clientSocket.Close();
BtnDC_Click(null, null);
StatusLabel1.Text = err.Message;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void BtnSnd_Click(object sender, EventArgs e)
{

// Text message to put into the send buffer
string textMessage = TxtbxSend.Text.Trim();
// Format the string message into the send buffer
FormatBuffer(sendBuffer, textMessage + "\r\n");
// Send the request to the server
rc = clientSocket.Send(sendBuffer);
StatusLabel1.Text = "Sent request of " + rc + " bytes ";
TxtbxSend.Clear();

}
private void BtnExit_Click(object sender, EventArgs e)
{
// For TCP, shutdown sending on our side since the client won't send any more data
BtnDC_Click(null, null);
Application.ExitThread();
System.Environment.Exit(System.Environment.ExitCod e);
Application.Exit();
}
private void BtnConnect_Click(object sender, EventArgs e)
{
readThread = new Thread(new ThreadStart(RunClient));
readThread.Start();
BtnDC.Enabled = true;
BtnSnd.Enabled = true;
BtnConnect.Enabled = false;
}
private void BtnDC_Click(object sender, EventArgs e)
{
try
{
if (clientSocket != null)
{// For TCP, shutdown sending on our side since the client won't send any more data
clientSocket.Shutdown(SocketShutdown.Send);
}
}
catch (Exception err)
{
StatusLabel1.Text = err.Message;
}
BtnConnect.Enabled = true;
BtnSnd.Enabled = false;
}
private void TxtbxSend_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
BtnSnd_Click(null, null);
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
BtnExit_Click(null, null);
}
}
}

esmaeily-hosein
شنبه 23 خرداد 1388, 12:33 عصر
میتونی برای خودت یه فرمت درست کنی و بعد data های اومده را پارس کنی .

مثلا :
MESSAGE:{0},Hash:{1}
0: متن داخل
1: متن Hash شده .

اونطرف

message=stReceived.split(',',0);
hash=stReceived.spli(',',1);

اگر میخوای مطمین شی داده ها به طور کامل رسیده یا نه از پروتکل tcp استفاده کنی . چون خودش برات اینکار میکنه .