Reputation: 1799
I know this is probably too general of a question, but what is the concept behind socket programming in C#? I was able to send/receive data with myself using the code below:
//client side?
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace client1
{
class Program
{
private static Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
Console.Title = "Client";
LoopConnect();
SendLoop();
}
private static void SendLoop()
{
while (true)
{
Console.Write("Enter a request: ");
string req = Console.ReadLine();
byte[] buffer = Encoding.ASCII.GetBytes(req);
_clientSocket.Send(buffer);
byte[] receivedBuf = new byte[1024];
int rec = _clientSocket.Receive(receivedBuf);
byte[] data = new byte[rec];
Array.Copy(receivedBuf, data, rec);
Console.WriteLine("Received: " + Encoding.ASCII.GetString(data));
}
}
private static void LoopConnect()
{
int attempts = 0;
while (!_clientSocket.Connected)
{
try
{
attempts++;
_clientSocket.Connect(IPAddress.Loopback, 100);
}
catch (SocketException)
{
Console.Clear();
Console.WriteLine("Connection attempts: " + attempts.ToString());
}
}
Console.Clear();
Console.WriteLine("Connected");
}
}
}
//server side?
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace server1
{
class Program
{
private static List<Socket> _clientSockets = new List<Socket>();
private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static byte[] _buffer = new byte[1024];
static void Main(string[] args)
{
Console.Title = "Server";
SetupServer();
Console.ReadLine();
}
private static void SetupServer()
{
Console.WriteLine("Setting up server...");
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
_serverSocket.Listen(5);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void AcceptCallback(IAsyncResult AR)
{
Socket socket = _serverSocket.EndAccept(AR);
_clientSockets.Add(socket);
Console.WriteLine("Client connected...");
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void ReceiveCallback(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
int received = socket.EndReceive(AR);
byte[] dataBuf = new byte[received];
Array.Copy(_buffer, dataBuf, received);
string text = Encoding.ASCII.GetString(dataBuf);
Console.WriteLine("Text received: " + text);
string response = string.Empty;
if (text.ToLower() != "get time")
{
response = "Invalid Request";
} else
{
response = DateTime.Now.ToLongDateString();
}
//if (text.ToLower() == "get time")
//{
//SendText(DateTime.Now.ToLongTimeString());
//byte[] data = Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString());
//socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
//}
byte[] data = Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString());
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
}
private static void SendCallback(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
socket.EndSend(AR);
}
}
}
However, when I gave the client.exe to my friend who is on the same network while I was running the server.exe, he was not able to connect to me. Does anyone have any suggestions as to what I'm doing wrong?
Upvotes: 3
Views: 956
Reputation:
Based on the current code in your question, the client tries to connect to the loopback (127.0.0.1
), so it doesn't work if the server is installed on a different machine.
If that was a typo and you've correctly configured the IP address, please double check that no firewall is blocking the requests.
Upvotes: 1