-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic_Socket_Programming_Client
More file actions
45 lines (40 loc) · 1.49 KB
/
Basic_Socket_Programming_Client
File metadata and controls
45 lines (40 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace Server
{
class Program
{
static byte[] Buffer { get; set; }
//buffer is initialized
static Socket s;
static void Main(string[] args)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(new IPEndPoint(0,1234));
//bind socket to certain endpoint
s.Listen(100);
Socket accepted = s.Accept();
//blocking call and wait for an available socket that attempts to connect and will transfer socket over this variable
Buffer = new byte[accepted.SendBufferSize];
//the default buffer size is 8192...no. of bytes that can be recieved at one time
int bytes = accepted.Receive(Buffer);
//recieve ...wait for a buffer .. recieve it and transfer to buffer variable.. returns bytes that read
byte[] formatted = new byte[bytes];
//format the buffer to free the buffer
for (int i = 0; i < bytes; i++)
{
formatted[i] = Buffer[i];
}
string strdata = Encoding.ASCII.GetString(formatted);
Console.WriteLine(strdata);
Console.Read();
s.Close();
accepted.Close();
}
}
}