Carloine menths

Carloine menths

  • 1.8k
  • 41
  • 10.6k

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
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Sockets;  
  6. using System.Text;  
  7. using System.Threading.Tasks;
  8. namespace Server  
  9. {  
  10.     class Program  
  11.     {  
  12.         
  13.         static void Main(string[] args)  
  14.         {  
  15.   
  16.             IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];  
  17.             TcpListener server = new TcpListener(ip, 8080);  
  18.             TcpClient client = default(TcpClient);  
  19.             try  
  20.             {  
  21.                 server.Start();  
  22.                 Console.WriteLine("Démarrage du Serveur");  
  23.                 Console.Read();  
  24.             }  
  25.             catch(Exception ex){  
  26.               
  27.                 Console.WriteLine(ex.ToString());  
  28.                 Console.Read();  
  29.             }  
  30.   while(true)
  31.             {  
  32.                 client = server.AcceptTcpClient();  
  33.                 byte[] receivedBuffer = new byte[1024];  
  34.                 NetworkStream stream = client.GetStream();  
  35.                 stream.Read(receivedBuffer, 0, receivedBuffer.Length);  
  36.                 string msg = Encoding.ASCII.GetString(receivedBuffer, 0, receivedBuffer.Length);  
  37.                 Console.Write(msg);  
  38.                 Console.Read();  
  39.   
  40.                   
  41.   
  42.             }  
  43.   
  44.   
  45.         }  
  46.     }  

Client Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Net;  
  11. using System.Net.Sockets;  
  12.   
  13. namespace Client  
  14. {  
  15.     public partial class Form1 : Form  
  16.          
  17.   
  18.     {  
  19.         string serverIP = "localhost";  
  20.         int port = 8080;  
  21.         public Form1()  
  22.         {  
  23.             InitializeComponent();  
  24.         }  
  25.   
  26.         private void label1_Click(object sender, EventArgs e)  
  27.         {  
  28.   
  29.         }  
  30.   
  31.         private void label3_Click(object sender, EventArgs e)  
  32.         {  
  33.   
  34.         }  
  35.   
  36.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
  37.         {  
  38.               
  39.         }  
  40.   
  41.         private void button1_Click(object sender, EventArgs e)  
  42.         {  
  43.   
  44.             TcpClient  client = new TcpClient(serverIP, port);  
  45.             
  46.             int byteCount = Encoding.ASCII.GetByteCount(NumCmp.Text);  
  47.             byte[] sendData = new byte[byteCount];  
  48.   
  49.             NetworkStream stream = client.GetStream();  
  50.             stream.Write(sendData, 0, sendData.Length);  
  51.             stream.Close();  
  52.             client.Close();  
  53.   
  54.   
  55.   
  56.   
  57.         }  
  58.   
  59.         private void textBox1_TextChanged(object sender, EventArgs e)  
  60.         {  
  61.   
  62.         }  
  63.     }  

 
 

Answers (1)