C_TcpClient(客户端)_and_TcpServer(服务端).doc
文本预览下载声明
C_TcpClient(客户端)_and_TcpServer(服务端)
C#:TcpClient(客户端) and TcpServer(服务端)
服务端:
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Net.Sockets;
using System.Net;
namespace PictureClient
{
/// summary
/// Interaction logic for PictureClientWindow.xaml
/// /summary
public partial class PictureClientWindow : Window
{
private const int bufferSize = 8192;
public PictureClientWindow()
{
InitializeComponent();
}
private TcpClient ConnectToServer()
{
TcpClient client = new TcpClient();
IPHostEntry host = Dns.GetHostEntry(Properties.Settings.Default.Server);
var address = (from h in host.AddressList where h.AddressFamily == AddressFamily.InterNetwork select h).First();
client.Connect( address.ToString(), Properties.Settings.Default.ServerPort);
return client;
}
private void buttonGetPictureList_Click(object sender, RoutedEventArgs e)
{
//send data
TcpClient client = ConnectToServer();
NetworkStream clientStream = client.GetStream();
string request = LIST;
byte[] requestBuffer = Encoding.ASCII.GetBytes(request);
clientStream.Write(requestBuffer, 0, requestBuffer.Length);
//read response
byte[] responseBuffer = new byte[bufferSize];
Memory
显示全部