How to solve it?
Use asynchronous sockets. See the reference documentation.
Asynchronous Server Socket Example
Asynchronous Client Socket Example
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have to application one of them is client other server .
My server application codes:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace Server_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TcpListener server;
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)//Start Butonu
{
textBox1.Text = "Server starting";
TcpClient client;
try
{
Byte[] bytes = new Byte[256];
String data = null;
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("192.168.5.129");
server = new TcpListener(localAddr, port);
server.Start();//Burası OK
client = default(TcpClient);
client = server.AcceptTcpClient(); //Burası OK
textBox1.Text += " Connected\n";
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
txtStatus.Text += "\n";
txtStatus.Text += " Received: ";
txtStatus.Text += data;
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
textBox2.Text += " Sent: ";
textBox2.Text += data;
//stream.Flush(); hiç bir numarası yok
//client.Close();
}
if(stream == null) { client.Close(); } //burası durmalı
}
catch (SocketException ex)
{
Console.WriteLine("SocketException: {0}",ex);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
server.Stop();
}
}
}
}
My client application codes :
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
NetworkStream stream;
TcpClient client;
String[] x = { "0x01 ,0x96 ,0x03 ,Data ,Data ,0x08 ,Dataya Göre Hesaplanir\n",
"0x01 ,0x70 ,0x02 ,Data ,0x01 ,0x08 ,Dataya Göre Hesaplanir\n",
"0x01 ,0x96 ,0x03 ,Data ,Data ,0x08 ,Dataya Göre Hesaplanir\n",
"0x01 ,0x70 ,0x02 ,Data ,0x01 ,0x08 ,Dataya Göre Hesaplanir\n",
};
public void DecToHex(int decn) //decimal to hexadecimal
{
int decimalNumber, quotient;
int i = 1, j, temp = 0;
char[] hexadecimalNumber = new char[100];
char temp1;
decimalNumber = decn;
quotient = decimalNumber;
while (quotient != 0)
{
temp = quotient % 16;
if (temp < 10)
temp = temp + 48;
else
temp = temp + 55;
temp1 = Convert.ToChar(temp);
hexadecimalNumber[i++] = temp1;
quotient = quotient / 16;
}
for (j = i - 1; j > 0; j--)
textBox2.Text += hexadecimalNumber[j];
}
public void HexToDec(int m)//hexadecimal to decimal
{
Byte[] b = new Byte[16];
b[0] = Convert.ToByte(m.ToString(), 16);//hexadecimale dönüştürme
textBox2.Text +=b[0];
}
public void myFunction(String s,int x,int y)
{
Random random = new Random();
int num = random.Next(x, y);
String str = num.ToString();
try
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes(s + str);
stream = client.GetStream();
stream.Write(data, 0, data.Length);
txtStatus.Text += "\nSend: ";
txtStatus.Text += s + str;
stream.Flush();
data = new Byte[256];
String responseData = String.Empty;
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
textBox2.Text += "\nReceived: ";
textBox2.Text += responseData;
client.Close();
}
catch(ObjectDisposedException e)
{
Console.WriteLine(e.ToString());
}
}
private void btnConnect_Click(object sender, EventArgs e)//hexadecimal to decimal yapar
{
// HexToDec(20);
client = new TcpClient("192.168.5.129",13000);
//client.Connect("192.168.5.129", 13000);
if (client.Connected)
{
textBox1.Text += "Bağlandı.";
}
else if (!client.Connected)
{
textBox1.Text += "Bağlantı Hatası";
}
}
private void btnSend_Click(object sender, EventArgs e)
{
myFunction(x[0],20,30);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)//işletim durumu mod seçimi
{
myFunction(x[1],17,59);
}
private void button2_Click(object sender, EventArgs e)
{
myFunction(x[1],14,19);
}
private void button5_Click(object sender, EventArgs e)
{
myFunction(x[1],18,16);
}
private void txtMessage_TextChanged(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)//dec to hex
{
DecToHex(32);
}
}
}
Connect BUTTON : Client Code (connects clients to server)
Start BUTTON : Server Code (STARTS THE SERVER)
Other buttons
I am pressing start button in server side then I am pressing connect button in client side to retrieve data from client , the process is successful but If I repeat this process I need to press start button again. I dont want to press start button and connect button again and again.
How to solve it?
Thank you
Elif.
How to solve it?
Use asynchronous sockets. See the reference documentation.
Asynchronous Server Socket Example
Asynchronous Client Socket Example