0
C#에서 비동기 TCP 서버 클라이언트를 작성했습니다. 하나의 컴퓨터에서 서버와 클라이언트를 동시에 테스트 할 때 두 프로그램이 모두 성공적으로 실행됩니다. 그러나 두 대의 다른 컴퓨터에서 테스트 할 때 서버 - 클라이언트 연결이 설정되고 클라이언트는 서버로 데이터를 보낼 수 있지만 서버는 클라이언트에 아무 것도 보내지 않은 것처럼 보입니다.TCP 클라이언트에서 서버로 데이터를 보낼 수있는 이유는 무엇입니까?
TCPView를 사용하여 서버와 클라이언트 모두 예상대로 데이터를 수신/보내고 있음을 알고 있습니다. 나는 그것들을 막는 방화벽이라고 생각했다. 나는 그것을 사용할 수 없게 만들었다. 내 코드에 무슨 문제가 있습니까?
서버 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
// Server code
namespace ChatConsole
{
class Program
{
static Socket serverSocket, clientSocket;
static byte[] buffer;
const int defaultBufferSize = 1024;
static void Main(string[] args)
{
try
{
Console.WriteLine("This is server.");
// Create a new server socket.
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Create IP End Point.
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 1001);
// Bind endpoint to socket.
serverSocket.Bind(ep);
// Start listening for incoming client.
serverSocket.Listen(4);
// Execute callback method when client request connection.
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
while (true) { }
}
catch(Exception ex)
{
Console.WriteLine("from Main(): " + ex.Message);
}
}
private static void AcceptCallback(IAsyncResult ar)
{
try
{
// Store client socket handle.
clientSocket = serverSocket.EndAccept(ar);
Console.WriteLine("Client {0} joined.", clientSocket.RemoteEndPoint);
// Welcome the client when connection established.
buffer = Encoding.ASCII.GetBytes("Hello from server!");
clientSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
// Begin receiving data from connected client.
buffer = new byte[1024];
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch (Exception ex)
{
Console.WriteLine("from AcceptCallback(): " + ex.Message);
}
}
private static void SendCallback(IAsyncResult ar)
{
// Terminate current send session.
clientSocket.EndSend(ar);
Console.WriteLine("Replied.");
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Store the length of received data.
int received = clientSocket.EndReceive(ar);
if (received == 0)
return;
// Convert the received data to string then print in the console.
Array.Resize<byte>(ref buffer, received);
string text = Encoding.ASCII.GetString(buffer);
Console.WriteLine(text);
Array.Resize<byte>(ref buffer, defaultBufferSize);
// Send back message from the client.
byte[] data = Encoding.ASCII.GetBytes("from server: " + text);
clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch (Exception ex)
{
Console.WriteLine("from ReceiveCallback(): " + ex.Message);
}
}
}
}
클라이언트 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
// Client code
namespace ChatConsoleClient
{
class Program
{
static Socket clientSocket;
static IPEndPoint ep;
static byte[] buffer;
const int defaultBufferSize = 1024;
static void Main(string[] args)
{
try
{
Console.WriteLine("This is client.");
// Create new socket for client.
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Ask for server IP address.
string IP = Console.ReadLine();
// Create endpoint to be connected.
ep = new IPEndPoint(IPAddress.Parse(IP), 1001);
// Start connecting to the server.
clientSocket.BeginConnect(ep, new AsyncCallback(ConnectCallback), null);
buffer = new byte[1024];
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
while (true)
{
string text = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(text);
clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
}
}
catch (Exception ex)
{
Console.WriteLine("from Main(): " + ex.Message);
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
int received = clientSocket.EndReceive(ar);
if (received == 0)
return;
// Convert the received data to string then print in the console.
Array.Resize<byte>(ref buffer, received);
string text = Encoding.ASCII.GetString(buffer);
Console.WriteLine(text);
Array.Resize<byte>(ref buffer, defaultBufferSize);
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
private static void SendCallback(IAsyncResult ar)
{
clientSocket.EndSend(ar);
}
private static void ConnectCallback(IAsyncResult ar)
{
clientSocket.EndConnect(ar);
}
}
}
당신을 했 작동 서버 메시지에서 인사 하시겠습니까? –
예, 귀하의 경우 방화벽 차단 서버 포트를 확인하시기 바랍니다. – TsunamiCoder