Witam. Jako programista pythona mam kilka problemów z przyzwyczajeniem się do c#.. Pisze mały program który łączy się z serwerem, ma nasłuchiwać czy serwer nic mu nie wysyła a po naciśnięciu przycisku coś wysłać do serwera. Kod aktualnie wygląda tak:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
namespace TrClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Listen{
public void Beta()
{
while (true)
{
try
{
Int32 port = 800;
TcpClient client = new TcpClient("localhost", port);
string message = "bla";
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
void SendTo()
{
this.Beta().stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
}
void Recive(){
}
}
catch
{
MessageBox.Show("Serwer nie odpowiada :(");
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Listen rusted = new Listen();
rusted.Beta();
}
}
}
Starałem się jakoś na wątkach to zrobić ale coś mi nie wychodzi. Pozdrawiam i czekam na odpowiedź :)