Carloine menths

Carloine menths

  • NA
  • 41
  • 30.8k

TCP/IP Client send data to server

Feb 14 2018 12:07 PM

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

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

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)  
        {
        }  
    }  
} 

 


Answers (2)