?? wintalk.cs
字號(hào):
/*=====================================================================
File: Wintalk.cs
Summary: Demonstrates how to create a socket chat application
using various .NET Framework library types.
---------------------------------------------------------------------
This file is part of the Microsoft .NET Framework SDK Code Samples.
Copyright (C) 2000 Microsoft Corporation. All rights reserved.
This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation. See these other
materials for detailed information regarding Microsoft code samples.
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
=====================================================================*/
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Windows.Forms;
class App{
// Entry point
public static void Main(String[] args){
// If the args parse in known way then run the app
if(ParseArgs(args)){
// Create a custom Talker object
Talker talker = new Talker(endPoint, client);
// Pass the object reference to a new form object
TalkForm form = new TalkForm(talker);
// Start the talker "talking"
talker.Start();
// Run the applications message pump
Application.Run(form);
}
}
// Parsed Argument Storage
private static IPEndPoint endPoint;
private static bool client;
// Parse command line arguments
private static bool ParseArgs(String[] args){
try{
if(args.Length == 0){
client = false;
endPoint = new IPEndPoint(IPAddress.Any,5000);
return true;
}
if (args[0][0]!='/' && args[0][0]!='-')
{
ShowUsage();
return false;
}
switch(Char.ToUpper(args[0][1], CultureInfo.InvariantCulture)){
case 'L':
int port = 5000;
if(args.Length > 1){
port = Convert.ToInt32(args[1]);
}
endPoint = new IPEndPoint(IPAddress.Any,port);
client = false;
break;
case 'C':
port = 5000;
String address = "127.0.0.1";
client = true;
if(args.Length > 1){
address = args[1];
port = Convert.ToInt32(args[2]);
}
endPoint = new IPEndPoint(Dns.Resolve(address).AddressList[0], port);
break;
default:
ShowUsage();
return false;
}
}catch{
ShowUsage();
return false;
}
return true;
}
// Show sample usage
private static void ShowUsage(){
MessageBox.Show("WinTalk [switch] [parameters...]\n\n"+
" /L [port]\t\t-- Listens on a port. Default: 5000\n"+
" /C [address] [port]\t-- Connects to an address and port.\n\n"+
"Example Server - \n"+
"Wintalk /L\n\n"+
"Example Client - \n"+
"Wintalk /C ServerMachine 5000","WinTalk Usage");
}
}
// UI class for the sample
class TalkForm:Form {
public TalkForm(Talker talker) {
// Associate for method with the talker object
this.talker = talker;
talker.Notifications += new
Talker.NotificationCallback(HandleTalkerNotifications);
// Create a UI elements
Splitter talkSplitter = new Splitter();
Panel talkPanel = new Panel();
receiveText = new TextBox();
sendText = new TextBox();
// we'll support up to 64k data in our text box controls
receiveText.MaxLength = sendText.MaxLength = 65536;
statusText = new Label();
// Initialize UI elements
receiveText.Dock = DockStyle.Top;
receiveText.Multiline = true;
receiveText.ScrollBars = ScrollBars.Both;
receiveText.Size = new Size(506, 192);
receiveText.TabIndex = 1;
receiveText.Text = "";
receiveText.WordWrap = false;
receiveText.ReadOnly = true;
talkPanel.Anchor = (AnchorStyles.Top|AnchorStyles.Bottom
|AnchorStyles.Left|AnchorStyles.Right);
talkPanel.Controls.AddRange(new Control[] {sendText,
talkSplitter,
receiveText});
talkPanel.Size = new Size(506, 371);
talkPanel.TabIndex = 0;
talkSplitter.Dock = DockStyle.Top;
talkSplitter.Location = new Point(0, 192);
talkSplitter.Size = new Size(506, 6);
talkSplitter.TabIndex = 2;
talkSplitter.TabStop = false;
statusText.Dock = DockStyle.Bottom;
statusText.Location = new Point(0, 377);
statusText.Size = new Size(507, 15);
statusText.TabIndex = 1;
statusText.Text = "Status:";
sendText.Dock = DockStyle.Fill;
sendText.Location = new Point(0, 198);
sendText.Multiline = true;
sendText.ScrollBars = ScrollBars.Both;
sendText.Size = new Size(506, 173);
sendText.TabIndex = 0;
sendText.Text = "";
sendText.WordWrap = false;
sendText.TextChanged += new EventHandler(HandleTextChange);
sendText.Enabled = false;
AutoScaleBaseSize = new Size(5, 13);
ClientSize = new Size(507, 392);
Controls.AddRange(new Control[] {statusText,
talkPanel});
Text = "WinTalk";
}
// When the app closes, dispose of the talker object
protected override void OnClosed(EventArgs e){
if(talker!=null){
// remove our notification handler
talker.Notifications -= new
Talker.NotificationCallback(HandleTalkerNotifications);
talker.Dispose();
}
base.OnClosed(e);
}
// Handle notifications from the talker object
private void HandleTalkerNotifications(
Talker.Notification notify, Object data){
switch(notify){
case Talker.Notification.Initialized:
break;
// Respond to status changes
case Talker.Notification.StatusChange:
Talker.Status status = (Talker.Status)data;
statusText.Text = String.Format("Status: {0}", status);
if(status == Talker.Status.Connected){
sendText.Enabled = true;
sendText.Focus();
}
break;
// Respond to received text
case Talker.Notification.ReceivedAppend:
receiveText.AppendText(data.ToString());
//receiveText.SelectionStart = Int32.MaxValue;
//receiveText.ScrollToCaret();
break;
case Talker.Notification.ReceivedRefresh:
receiveText.Text = data.ToString();
receiveText.SelectionStart = Int32.MaxValue;
receiveText.ScrollToCaret();
break;
// Respond to error notifications
case Talker.Notification.Error:
Close(data.ToString());
break;
// Respond to end
case Talker.Notification.End:
// Don't send any more text
sendText.TextChanged -= new EventHandler(HandleTextChange);
MessageBox.Show(this, data.ToString(), "Closing WinTalk");
Close();
break;
default:
Close();
break;
}
}
// Handle text change notifications and send talk
private void HandleTextChange(Object sender, EventArgs e){
if(talker != null){
talker.SendTalk((sender as TextBox).Text);
}
}
// Close with an explanation
private void Close(String message){
MessageBox.Show(message, "Error!");
Close();
}
// Private UI elements
private TextBox receiveText;
private TextBox sendText;
private Label statusText;
private Talker talker;
}
// An encapsulation of the Sockets class used for socket chatting
class Talker:IDisposable{
// Construct a talker
public Talker(IPEndPoint endPoint, bool client){
this.endPoint = endPoint;
this.client = client;
socket = null;
reader = null;
writer = null;
statusText = prevSendText = prevReceiveText = String.Empty;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -