PDA

View Full Version : سوال: سوکت



mona11
پنج شنبه 07 دی 1391, 10:54 صبح
سلام.یه کد نوشتم تو مایه های چت...ولی خیلی ابتدائیه...مشکلش اینجاست که از سرور پیام رو ارسال میکنه،ولی کلاینت ارسال نمیکنه...
کد سرور

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

public class serv
{
public static bool Send = true;
public static bool Ricieve = true;
public static string s1 = "";



public static void Main()
{

Thread MainThread1 = new Thread(new ThreadStart(startProcessSend));
Thread MainThread2 = new Thread(new ThreadStart(startProcessRicieve));

MainThread1.Name = "Sender";

MainThread2.Name = "Reciever";

MainThread1.Start();

MainThread2.Start();


}
//************************************************** *************
public static void startProcessSend()
{
TcpListener myList = new TcpListener( IPAddress.Parse("127.0.0.1"), 8001);
myList.Start();
Socket s = myList.AcceptSocket();

ASCIIEncoding asen = new ASCIIEncoding();
Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
while (true)
{
Console.Write("Please enter text: \n");
s1 = Console.ReadLine();

s.Send(asen.GetBytes(s1));

}
}
//************************************************** *************
public static void startProcessRicieve()
{
TcpListener myList1 = new TcpListener(IPAddress.Parse("127.0.0.1"), 8001);
myList1.Start();
Socket s1 = myList1.AcceptSocket();
byte[] b = new byte[100];
string ss = "";
int k = 0;
while (true)
{
ss = "";
k = s1.Receive(b);
for (int i = 0; i < k; i++)
ss += Convert.ToChar(b[i]);
Console.WriteLine(ss);

}
}
}

کد کلاینت

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Threading;


public class clnt
{
public static TcpClient tcpclnt = new TcpClient();

//************************************************** **
public static void Main()
{
tcpclnt.Connect("127.0.0.1", 8001);
Thread MainThread1 = new Thread(new ThreadStart(startProcessSend));
Thread MainThread2 = new Thread(new ThreadStart(startProcessRicieve));

MainThread1.Name = "Sender";

MainThread2.Name = "Reciever";
{
MainThread1.Start();
MainThread2.Start();

}
}
//************************************************** ****************
public static void startProcessSend()
{

String str = "";
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
while (true)
{
str = Console.ReadLine();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
}

}
//************************************************** ************

public static void startProcessRicieve()
{

Stream stm = tcpclnt.GetStream();
byte[] bb = new byte[2000];
int k = 0;

string s2 = "";
while (true)
{
s2 = "";
k = stm.Read(bb, 0, 2000);
for (int i = 0; i < k; i++)
s2 += (Convert.ToChar(bb[i]));
Console.WriteLine(s2);

}

}

}

mona11
پنج شنبه 07 دی 1391, 14:30 عصر
یعنی تو ایران یکی نیست جواب بده؟:)