HeIlo
I want to send data from an application form to my server .this is my code I have an error I can't figure out what it is , the server console even when I click the button don't display the data.
Server Code
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Server
{
class Program
{
static void Main(string[] args)
{
IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener server = new TcpListener(ip, 8080);
TcpClient client = default(TcpClient);
try
{
server.Start();
Console.WriteLine("Démarrage du Serveur");
Console.Read();
}
catch(Exception ex){
Console.WriteLine(ex.ToString());
Console.Read();
}
while(true)
{
client = server.AcceptTcpClient();
byte[] receivedBuffer = new byte[1024];
NetworkStream stream = client.GetStream();
stream.Read(receivedBuffer, 0, receivedBuffer.Length);
string msg = Encoding.ASCII.GetString(receivedBuffer, 0, receivedBuffer.Length);
Console.Write(msg);
Console.Read();
}
}
}
}Client Code
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Client
{
public partial class Form1 : Form
{
string serverIP = "localhost";
int port = 8080;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient(serverIP, port);
int byteCount = Encoding.ASCII.GetByteCount(NumCmp.Text);
byte[] sendData = new byte[byteCount];
NetworkStream stream = client.GetStream();
stream.Write(sendData, 0, sendData.Length);
stream.Close();
client.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Vishal JadavRank #1,691 · 901 reputationPosted May 21, 2018, 4:29 AM EDT
Calin Corcotoi70 reputationPosted Feb 27, 2023, 2:14 PM EST
TcpClient client = new TcpClient(serverIP, port); int byteCount = Encoding.ASCII.GetByteCount(NumCmp.Text); byte[] sendData = new byte[byteCount]; sendData = Encoding.ASCII.GetBytes(NumCmp.Text); NetworkStream stream = client.GetStream(); stream.Write(sendData, 0, sendData.Length); stream.Close(); client.Close();add the bold line to see the text in the server