enviando el valor del cuadro de lista a la aplicación de la consola usando la programación de socket usando c #

If I don't put a $ symbol at the end of string in the textbox it throws an error saying I have to make a change in the code so that whatever the user puts in the textbox it should be displayed. This is one scenario.

In my second scenario I need to send the number of selected items from the listbox to console.

How do I store selected values in a variable?

This is my client side code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Net.Sockets;


namespace eg_client
{
    public partial class Form1 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            NetworkStream serverStream = clientSocket.GetStream();
            string data = textBox2.Text;
            byte[] dataB = System.Text.Encoding.Unicode.GetBytes(data);
            serverStream.Write(dataB, 0, dataB.Length);
            serverStream.Flush();
            byte[] inStream = new byte[10025];
            serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
            clientSocket.Connect("127.0.0.1", 8001);
            label2.Text = "Client Socket Program - Server Connected ...";
        }
    }
}

Este es mi código del lado del servidor

using System;
using System.Net.Sockets;
using System.Text;

namespace eg_server
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(8001);
            TcpClient clientSocket = default(TcpClient);
            serverSocket.Start();
            Console.WriteLine(" >> Server Started");
            clientSocket = serverSocket.AcceptTcpClient();
            Console.WriteLine(" >> Accept connection from client");
            while ((true))
            {
                try
                {
                    NetworkStream networkStream = clientSocket.GetStream();
                    byte[] bytesFrom = new byte[10025];
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    Console.WriteLine(" >> Data from client - " + dataFromClient);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> exit");
            Console.ReadLine();
        }
    }
}

¿Puede alguien ayudarme con esto?

preguntado el 28 de agosto de 12 a las 10:08

1 Respuestas

Your server side code has

dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

You have assumed the string will contain one, you need to decide what to do if the string does not have one.. such as

string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
if (dataFromClient.Contains("$"))
{
 dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
 Console.WriteLine(" >> Data from client - " + dataFromClient);
}
else 
 console.WriteLine("Data received in incorrect format");

Respondido 28 ago 12, 10:08

at the end of the string it should contain $ symbol right but i don't want to do in that way.I just want to display string that is entered in the textbox for eg hello to console - Deepu

Well, its up to you how to handle it thats why you're the coder. You didnt explain what you wanted, I just answered why you were getting the problem and the problem was there isnt necessarily an indexof("$") in your string. - Buscador de errores

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.