C# Socket Server Client Application
C# da Consol Application da Socket yardımıyla Server Client uygulamamız
SERVER KISMI
namespace Server
{
class Program
{
static void Main(string[] args)
{
//Dinlenecek Port
TcpListener tcpListener = new TcpListener(1234);
tcpListener.Start(); while (true)
{
Socket socket = tcpListener.AcceptSocket();
if (socket.Connected)
{
Console.WriteLine("Client Baglandı");
NetworkStream networkStream = new NetworkStream(socket);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
string mesaj = "Gönderiliyor";
streamWriter.WriteLine(mesaj);
Console.WriteLine(mesaj);
streamWriter.Flush();
mesaj = streamReader.ReadLine();
Console.WriteLine(mesaj);
streamReader.Close();
networkStream.Close();
streamWriter.Close();
}
}
//socket.Close();
Console.WriteLine("Cikis yapılıyor");
}
}
}
BU Classımızda Client Kısmı
namespace Client
{
class Program
{
static void Main(string[] args)
{
TcpClient clientbaglanti;
try
{
clientbaglanti = new TcpClient("localhost", 1234);
}
catch
{
Console.WriteLine(
"Server baglantı hatası {0}:999", "localhost");
return;
}
NetworkStream networkStream = clientbaglanti.GetStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
try
{
string mesaj;
string gonmsj = Console.ReadLine();
streamWriter.WriteLine(gonmsj);
mesaj = streamReader.ReadLine();
Console.WriteLine(mesaj);
streamWriter.Flush();
networkStream.Close();
Console.ReadLine();
}
catch
{
}
}
}
}
SERVER KISMI
namespace Server
{
class Program
{
static void Main(string[] args)
{
//Dinlenecek Port
TcpListener tcpListener = new TcpListener(1234);
tcpListener.Start(); while (true)
{
Socket socket = tcpListener.AcceptSocket();
if (socket.Connected)
{
Console.WriteLine("Client Baglandı");
NetworkStream networkStream = new NetworkStream(socket);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
string mesaj = "Gönderiliyor";
streamWriter.WriteLine(mesaj);
Console.WriteLine(mesaj);
streamWriter.Flush();
mesaj = streamReader.ReadLine();
Console.WriteLine(mesaj);
streamReader.Close();
networkStream.Close();
streamWriter.Close();
}
}
//socket.Close();
Console.WriteLine("Cikis yapılıyor");
}
}
}
BU Classımızda Client Kısmı
namespace Client
{
class Program
{
static void Main(string[] args)
{
TcpClient clientbaglanti;
try
{
clientbaglanti = new TcpClient("localhost", 1234);
}
catch
{
Console.WriteLine(
"Server baglantı hatası {0}:999", "localhost");
return;
}
NetworkStream networkStream = clientbaglanti.GetStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
try
{
string mesaj;
string gonmsj = Console.ReadLine();
streamWriter.WriteLine(gonmsj);
mesaj = streamReader.ReadLine();
Console.WriteLine(mesaj);
streamWriter.Flush();
networkStream.Close();
Console.ReadLine();
}
catch
{
}
}
}
}
Yorumlar
Yorum Gönder