-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
58 lines (41 loc) · 1.68 KB
/
Program.cs
File metadata and controls
58 lines (41 loc) · 1.68 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
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Net;
using System.Net.Sockets;
namespace SocketClient
{
internal class Program
{
static void Main(string[] args)
{
//Get the server IP address and the port number
String serverIP = "";
String serverPort = "";
Console.WriteLine("Enter the IP Address of the Server : ");
serverIP = Console.ReadLine();
Console.WriteLine("Enter the Port Number of the Server : ");
serverPort = Console.ReadLine();
// 1. Create the socket
Socket m_sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 2. Connect to the server
IPAddress destIP = IPAddress.Parse(serverIP);
int destPort = System.Convert.ToInt16(serverPort);
IPEndPoint destEP = new IPEndPoint(destIP, destPort);
// User Message
Console.WriteLine("Waiting to Connect..");
m_sendSocket.Bind(destEP);
Console.WriteLine("Connected.");
// 3. Send Data
string msg;
Console.WriteLine("Enter message to send : ");
msg = Console.ReadLine();
Byte[] s_data = System.Text.Encoding.ASCII.GetBytes(msg);
// User Message
Console.WriteLine("Sending Data..");
m_sendSocket.Send(s_data, SocketFlags.None);
// User Message
Console.WriteLine("Sending Completed..");
// 4. Shutdown & Close Socket
m_sendSocket.Shutdown(SocketShutdown.Both);
m_sendSocket.Close();
}
}
}