2012-08-28 3 views
-1

을 사용하여 콘솔 응용 프로그램에 목록 상자 값 보내기 텍스트 상자에 문자열 끝에 $ 기호를 삽입하지 않으면 사용자가 무엇이든간에 코드를 변경해야한다는 오류가 발생합니다. 텍스트 상자에 넣으십시오. 이것은 하나의 시나리오입니다.소켓 프로그래밍을 사용하여 C#

두 번째 시나리오에서는 목록 상자에서 선택한 항목의 수를 콘솔로 보내야합니다.

변수에 선택한 값을 저장하려면 어떻게합니까?

이 내 클라이언트 측 코드

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 ..."; 
     } 
    } 
} 

이 내 서버 측 코드가

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(); 
     } 
    } 
} 

이 사람이 나를 도와 줄 수있다인가?

답변

0

서버 측 코드 당신은 문자열을 포함합니다 가정 하였다

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

있다, 당신은 같은

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"); 
+0

에서 .. 문자열이 하나가없는 경우 어떻게 할 것인지를 결정해야 문자열의 끝은 $ symbol right를 포함해야하지만 그렇게하고 싶지는 않습니다. 텍스트 상자에 입력 된 문자열을 표시하고 싶습니다. 예를 들어 콘솔에 hello를 입력하십시오. – Deepu

+0

글쎄, 어떻게 처리할까요? 그것은 당신이 코더 인 이유입니다. 당신은 당신이 원했던 것을 설명하지 못했고, 나는 단지 당신이 문제를 얻는 이유와 그 문제가 반드시 당신의 문자열에서 indexof ("$")가 아니었던 이유에 대해 대답했습니다. – BugFinder

관련 문제