PDA

View Full Version : سوال: Thread و برنامه نویسی شبکه



reza_program
دوشنبه 17 خرداد 1389, 09:03 صبح
سلام
من میخوام چندتا کلاینت به برنامه روی سرور وصل بشند و فقط سرور اطلاع بده که یه کلاینت وصل شد مثلا تو ی لیست باکس

یه کد پیدا کردم که این کارو تنجام میداد ولی کنسول بود

میخوام به Win App تبدیلش کنم:

حالا کار نمیکنه

اینم کد:

سرور:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;


namespace TSocket
{
public partial class Thread_Frm : Form
{
public int cnt = 0;

public Thread_Frm()
{
InitializeComponent();
}

private void Thread_Frm_Load(object sender, EventArgs e)
{
Thread listenthr = new Thread(new ThreadStart(listenTCP));
listenthr.Start();
}
ArrayList ClientList;
TcpListener server = new
TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), 8080);

public void listenTCP()
{

ClientList = new ArrayList();

server.Start();
try
{

while (true)
{


TcpClient handler = server.AcceptTcpClient();

int i = ClientList.Add(new ClientHandler(handler));
((ClientHandler)ClientList[i]).Start();

listBox1.Items.Add("This is C_" + i + ClientList);
foreach (ArrayList ip in ClientList)
{
MessageBox.Show(ip.ToString());
}

}

}
catch (Exception e)
{
MessageBox.Show(e.Message);
}

}

private void button1_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient();

client = server.AcceptTcpClient(); // Current process is block after this command (Better is in other thread).

byte[] sendBytes = Encoding.Unicode.GetBytes(textBox1.Text); // Bytes to send for client.
client.Client.Send(sendBytes, SocketFlags.None); // Send data to the client.
}
}
}

class ClientHandler
{

TcpClient ClientSocket;
bool ContinueProcess = false;
Thread ClientThread;

public ClientHandler(TcpClient ClientSocket)
{
this.ClientSocket = ClientSocket;
}

public void Start()
{
ContinueProcess = true;
ClientThread = new Thread(new ThreadStart(Process));
ClientThread.Start();
}

private void Process()
{

// Incoming data from the client.
string data = null;

// Data buffer for incoming data.
byte[] bytes;

if (ClientSocket != null)
{
NetworkStream networkStream = ClientSocket.GetStream();
ClientSocket.ReceiveTimeout = 100; // 1000 miliseconds


MessageBox.Show("Hello");



networkStream.Close();
ClientSocket.Close();
}
} // Process()


} // class ClientHandler





کلاینت:



using System;
using System.Net.Sockets;
using System.Text;

class TcpClientTest {

private const int portNum = 10116 ;

static public void Main() {

TcpClient tcpClient = new TcpClient();
try{
tcpClient.Connect("localhost", portNum);
NetworkStream networkStream = tcpClient.GetStream();

if (networkStream.CanWrite && networkStream.CanRead){

String DataToSend = "" ;

while ( DataToSend != "quit" ) {

Console.WriteLine("\nType a text to be sent:");
DataToSend = Console.ReadLine() ;
if ( DataToSend.Length == 0 ) break ;

Byte[] sendBytes = Encoding.ASCII.GetBytes(DataToSend);
networkStream.Write(sendBytes, 0, sendBytes.Length);

// Reads the NetworkStream into a byte buffer.
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
int BytesRead = networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);

// Returns the data received from the host to the console.
string returndata = Encoding.ASCII.GetString(bytes, 0 , BytesRead);
Console.WriteLine("This is what the host returned to you: \r\n{0}", returndata);
}
networkStream.Close();
tcpClient.Close();
}
else if (!networkStream.CanRead){
Console.WriteLine("You can not write data to this stream");
tcpClient.Close();
}
else if (!networkStream.CanWrite){
Console.WriteLine("You can not read data from this stream");
tcpClient.Close();
}
}
catch (SocketException) {
Console.WriteLine("Sever not available!");
}
catch (System.IO.IOException) {
Console.WriteLine("Sever not available!");
}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}
} // Main()
} // class TcpClientTest {



ممنون میشم جواب بدید!
یا علی