forked from XINCGer/Unity3DTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
67 lines (59 loc) · 2.03 KB
/
Copy pathProgram.cs
File metadata and controls
67 lines (59 loc) · 2.03 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
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SocketClient
{
class Program
{
/// <summary>
/// 数据缓冲区
/// </summary>
private static byte[] dataBuff = new byte[1024];
private static Socket socketClient;
private static int port = 6666;
static void Main(string[] args)
{
IPAddress ip = IPAddress.Loopback;
socketClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
socketClient.Connect(new IPEndPoint(ip,port));
Console.WriteLine("连接服务器成功!");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("连接服务器失败!按回车键退出!");
return;
}
//通过clientSocket接受数据
int receiveNum = socketClient.Receive(dataBuff);
Console.WriteLine("接收服务器消息:{0}",Encoding.UTF8.GetString(dataBuff,0,receiveNum));
//通过clientSocket发送消息
for (int i = 0; i < 10; i++)
{
try
{
Thread.Sleep(1000);
string message = string.Format("{0}{1}", "Server 你好!", DateTime.Now);
socketClient.Send(Encoding.UTF8.GetBytes(message));
Console.WriteLine("向服务器发送消息:{0}",message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
socketClient.Shutdown(SocketShutdown.Both);
socketClient.Close();
break;
}
}
Console.WriteLine("发送完毕!按回车键退出!");
Console.Read();
}
}
}