C#实现HelloWorld
1,c#创建控制台helloworld
编写代码
编译
实现窗体应用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
showMsg();
}
void showMsg()
{
//向文本控件中添加HelloWorld
textBox1.AppendText("Hello World!" + "\t\n");
}
}
}
C#控制台程序,利用UDP套接字实现消息的发送
服务器前段代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDP
{
class Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];
//得到本机IP,设置TCP端口号
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8001);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//绑定网络地址
newsock.Bind(ip);
Console.WriteLine("This is a Server, host name is {0}", Dns.GetHostName());
//等待客户机连接
Console.WriteLine("Waiting for a client");
//得到客户机IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
//客户机连接成功后,发送信息
string welcome = "你好 ! ";
//字符串与字节数组相互转换
data = Encoding.UTF8.GetBytes(welcome);
//发送信息
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
while (true)
{
data = new byte[1024];
//接收信息
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
//newsock.SendTo(data, recv, SocketFlags.None, Remote);
}
}
}
}
创建ip地址和端口:
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8001);
绑定监听地址:
newsock.Bind(ip);
连接
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
获取客户端的ip,来建立联系
接收客户端的发送过来的消息
recv = newsock.ReceiveFrom(data, ref Remote);
客户端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDPClient
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
string input, stringData;
//构建TCP 服务器
Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());
//设置服务IP(这个IP地址是服务器的IP),设置TCP端口号
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.43.98"), 8001);
//定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "你好! ";
data = Encoding.UTF8.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ip);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
int i = 0;
while (true)
{
string s = "hello cqjtu!重交物联2018级"+i;
Console.WriteLine(s);
server.SendTo(Encoding.UTF8.GetBytes(s),Remote);
if(i==50)
{
break;
}
i++;
}
Console.WriteLine("Stopping Client.");
server.Close();
}
}
}
实现客户端向服务器发送消息效果(消息一共50条,只截取部分内容)
服务器接收到客户端发送的消息:
客户端发送消息内容:
C#窗口程序,利用TCP套接字实现消息的发送
单线程
服务器代码
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.IO;
namespace WindowsServer2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//点击开始侦听的时候,服务器创建一个负责监听IP地址跟端口号的Socket
Socket socketwatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Any;
//创建端口对象
IPEndPoint point = new IPEndPoint(ip, 8001);
//绑定
socketwatch.Bind(point);
showMsg("监听成功!");
socketwatch.Listen(10);
//等待客户端的连接
Socket socketSend=socketwatch.Accept();
showMsg(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功!");
}
catch
{
showMsg("监听失败");
}
}
void showMsg(string str)
{
textBox1.AppendText(str + "\r\n");
}
}
}
测试是否能够连接到客户端
方法:在Windows下,打开cmd,输入telnet 你自己服务器IP地址 端口号
多线程
服务器代码:
using System;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
namespace WindowsServer2
{
public partial class server : Form
{
public server()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//点击开始侦听的时候,服务器创建一个负责监听IP地址跟端口号的Socket
Socket socketwatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Any;
//创建端口对象
IPEndPoint point = new IPEndPoint(ip, 8001);
//绑定
socketwatch.Bind(point);
showMsg("监听成功!");
socketwatch.Listen(10);
//创建一个线程
Thread th = new Thread(Listen);
th.IsBackground = true;
th.Start(socketwatch);
}
catch
{
showMsg("监听失败");
}
}
Socket socketSend;
//等待客户端的连接
void Listen(Object o)
{
try
{
Socket socketwatch = o as Socket;
while (true)
{
//等待客户端的连接
socketSend = socketwatch.Accept();
showMsg(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功!");
//开启一个新的线程不断的接收客户端信息
Thread th = new Thread(Receive);
th.IsBackground = true;
th.Start(socketSend);
}
}
catch
{ }
}
//接收客户端发送的信息
void Receive(Object o)
{
try
{
Socket socketSend = o as Socket;
while (true)
{
byte[] buffer = new byte[1024 * 1024 * 2];
int r = socketSend.Receive(buffer);
if (r == 0)
{
break;
}
string str = Encoding.UTF8.GetString(buffer, 0, r);
showMsg(socketSend.RemoteEndPoint + ":"+str);
}
}
catch
{ }
}
void showMsg(string str)
{
textBox1.AppendText(str + "\r\n");
}
//服务器给客户端发送消息
private void button2_Click(object sender, EventArgs e)
{
string str = textBox2.Text;
Console.WriteLine(str);
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
socketSend.Send(buffer);
}
}
}
客户端代码:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Windowsclient2
{
public partial class client : Form
{
public client()
{
InitializeComponent();
}
Socket socketSend;
private void button1_Click(object sender, EventArgs e)
{
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint point =new IPEndPoint(IPAddress.Parse("192.168.43.98"), 8001);
socketSend.Connect(point);
showMsg("连接成功!");
Thread th = new Thread(Receive);
th.IsBackground = true;
th.Start();
}
//客户端接收服务器发送的消息
void Receive()
{
try
{
while (true)
{
byte[] buffer = new byte[1024 * 1024 * 2];
int r = socketSend.Receive(buffer);
if (r == 0)
{
break;
}
string str = Encoding.UTF8.GetString(buffer, 0, r);
showMsg(socketSend.RemoteEndPoint + ":"+str);
}
}
catch
{ }
}
void showMsg(string s)
{
textBox1.AppendText(s + "\r\n");
}
//客户端向服务器发送消息
private void button2_Click(object sender, EventArgs e)
{
string str = textBox2.Text;
int j;
for (j = 0; j < 50; j++)
{
string str1 = str + j;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str1);
socketSend.Send(buffer);
}
}
}
}