이제 서버로 작업하려고합니다. 나는이 코드를 가지고있다 : 서버 연결 오류
'ParametrizedThreadStart'를 쓰레드에서 'HandleCC'메소드를 시작한다. 나는 '127.0.0.1'에서 연결을 시도하고있다. 나는 연결할 수있다. 연결되면 첫 번째 중단 점은 양호하지만 두 번째 중단 점은 코드 중단보다 좋습니다. (콘솔은 여전히 실행 중이지만 다음 중단 점에서 아무도 코드를 깨지 않습니다.) 도움을 주셔서 감사합니다. 내 영어를 유감스럽게 생각합니다.
전체 코드 : 당신은 클라이언트와 서버를 모두 필요한 모든 클라이언트 - 서버 응용 프로그램에서
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Threading;
namespace MTSP
{
class Comunication
{
private TcpListener listener;
private Thread thread;
public Comunication()
{
this.listener = new TcpListener(IPAddress.Any, 20345);
this.thread = new Thread(new ThreadStart(this.ListenLoop));
this.thread.Start();
}
private void ListenLoop()
{
this.listener.Start();
while (true)
{
TcpClient client = this.listener.AcceptTcpClient();
Thread tr = new Thread(new ParameterizedThreadStart(this.HandleCC));
tr.Start(client);
}
}
private void HandleCC(object client)
{
TcpClient cli = (TcpClient)client;
NetworkStream stream = cli.GetStream();
byte[] buffer = new byte[1024];
int bytesread = 0;
string mess = "";
StringBuilder compmess = new StringBuilder();
while (true)
{
bytesread = 0;
try
{
bytesread = stream.Read(buffer, 0, buffer.Length);
}
catch
{
break;
}
compmess.AppendFormat("{0}", Encoding.ASCII.GetString(buffer, 0, bytesread));
string a = compmess.ToString();
string g;
}
System.Diagnostics.Debug.WriteLine(compmess.ToString());
cli.Close();
}
}
}
어떻게 '클라이언트'를 만드나요? 어떤 서버에 연결하고 있습니까? 'Read()'가 리턴하지 않는 가장 큰 이유는 서버가 어떤 데이터도 보내지 않는다는 것입니다. 그리고'catch '와'break'를 사용하는 방식은 매우 잘못된 것처럼 보입니다. 또한 스크린 샷이 아닌 텍스트로 코드를 게시 할 수 있습니까? – svick
내 게시물에 있습니다. – user35443
멈추지 않으면 막습니다. 뭔가를 보내십시오. –