C#實(shí)現(xiàn)的Socket服務(wù)器端、客戶端代碼分享
更新時(shí)間:2014年08月06日 10:44:58 投稿:junjie
這篇文章主要介紹了C#實(shí)現(xiàn)的Socket服務(wù)器端、客戶端代碼分享,2個(gè)非常簡(jiǎn)單的入門例子,需要的朋友可以參考下
服務(wù)端:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
class Program
{
static void Main(string[] args)
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"),55555);
try
{
server.Bind(point);
server.Listen(10);
//監(jiān)聽本地端口
System.Console.WriteLine("開始監(jiān)聽本地端口:55555");
while (true)
{
Socket sock = server.Accept();
byte[] buffer = new byte[1024 * 1024];
int n = sock.Receive(buffer);
String cmd = Encoding.UTF8.GetString(buffer, 0, n);
String result = execCmd(cmd);
byte[] bytes = Encoding.UTF8.GetBytes(result);
sock.Send(bytes);
}
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
return;
}
}
//重定向輸入輸出流,并且用cmd執(zhí)行命令
private static String execCmd(String cmd)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments ="/c "+cmd;
//隱藏程序外殼
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//在這里重定向輸出即可,因?yàn)椴皇墙换ナ降?,如果需要交互式的直接反彈cmd即可
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
return p.StandardOutput.ReadToEnd();
}
}
}
客戶端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
/*
* Code By iswin
*/
namespace Client
{
public partial class main : Form
{
public main()
{
InitializeComponent();
this.ip.Text="127.0.0.1";
this.cmd.Text="ipconfig";
this.port.Text = "55555";
}
private void send_Click(object sender, EventArgs e)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
String remoteip=this.ip.Text;
String command = this.cmd.Text;
IPAddress ip = IPAddress.Parse(remoteip);
IPEndPoint point=new IPEndPoint(ip,int.Parse(port.Text));
try
{
this.recvmsg.Text = "開始連接服務(wù)端:" + remoteip + ":" + port.Text + "\n";
client.Connect(point);
this.recvmsg.Text="連接服務(wù)端!\n給服務(wù)端發(fā)送指令:"+command;
byte[] buffer = Encoding.UTF8.GetBytes(command);
//講輸入的指令發(fā)送到服務(wù)端
client.Send(buffer);
//接受服務(wù)端返回的數(shù)據(jù)
recvmsgs(client);
client.Close();
}
catch (Exception ex)
{
this.recvmsg.Text = ex.Message;
MessageBox.Show(ex.Message);
return;
}
}
//接受服務(wù)端發(fā)送來的消息
private void recvmsgs(Socket client)
{
try
{
byte[] buffer = new byte[1024 * 1024];
int size = client.Receive(buffer);
String recv = Encoding.UTF8.GetString(buffer, 0, size);
this.recvmsg.Text = "\n" + recv;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
}
您可能感興趣的文章:
- C#使用Socket發(fā)送和接收TCP數(shù)據(jù)實(shí)例
- c#(Socket)異步套接字代碼示例
- C#中Socket通信用法實(shí)例詳解
- C#使用Socket實(shí)現(xiàn)發(fā)送和接收?qǐng)D片的方法
- c#(Socket)同步套接字代碼示例
- C#開發(fā)之Socket網(wǎng)絡(luò)編程TCP/IP層次模型、端口及報(bào)文等探討
- C#中異步Socket通信編程代碼實(shí)例
- C#實(shí)現(xiàn)Socket通信的解決方法
- C# Socket網(wǎng)絡(luò)編程實(shí)例
- C#學(xué)習(xí)教程之Socket的簡(jiǎn)單使用
相關(guān)文章
C#數(shù)值轉(zhuǎn)換-隱式數(shù)值轉(zhuǎn)換表參考
隱式轉(zhuǎn)換就是直接使用,比如可以把一個(gè) byte 類型直接用在 int 上2013-04-04
C#中Thread(線程)和Task(任務(wù))實(shí)例詳解
.NET Framework在System.Threading命名空間中具有與線程相關(guān)的類,線程是一小組可執(zhí)行指令,這篇文章主要給大家介紹了關(guān)于C#中Thread(線程)和Task(任務(wù))的相關(guān)資料,需要的朋友可以參考下2022-03-03
Unity3D使用GL實(shí)現(xiàn)圖案解鎖功能
這篇文章主要為大家詳細(xì)介紹了Unity3D使用GL實(shí)現(xiàn)圖案解鎖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
C#實(shí)現(xiàn)路由器斷開連接,更改公網(wǎng)ip的實(shí)例代碼
C#實(shí)現(xiàn)路由器斷開連接,更改公網(wǎng)ip的實(shí)例代碼,需要的朋友可以參考一下2013-05-05
C#內(nèi)置隊(duì)列類Queue用法實(shí)例
這篇文章主要介紹了C#內(nèi)置隊(duì)列類Queue用法,實(shí)例分析了C#內(nèi)置隊(duì)列的添加、移除等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04

