PDA

View Full Version : Lập trình mạng trong .NET làm thế nào ?



tiensidien
19-04-2006, 00:29
Chào các bạn...
Mình muốn tìm hiểu về lập trình mạng trong .NEt
Mình biết cái .NET Framework nó hổ trợ rất nhiều nhưng không rành lắm

Không biết có bạn nào rành không ? Giúp mình một tí nhé

VD : Mình có 2 máy tính, máy A và máy B
Trên A chạy chương trình Server, trên B chạy chương trình Client
Làm sao để trên A mình thực hiện sự kiện Click lên Button thì lập tức máy B sẽ thực hiện một hành động nào đó vì dụ như hiển thị lên 1 Msgbox "ha ha ha"

Với đk là nó phải chạy theo kiểu realtime, giống như bộ đk từ xa, khi mình bấm nút chuyển kênh thì nó chuyển liền mà không có dộ trễ (trừ độ trễ do đường truyền của mạng).

nguoivodanh1003
19-04-2006, 09:59
Hahaha, liên lạc ras_it hay vào trong thread này: "Cần lấy IP/name của máy đang kết nối", đọc thêm đi.
Chúc thành công.

sangdqdt12
05-05-2006, 09:48
dùng lớp socket cua net frame là được luôn a.

đọc thử coi
using System;
using System.Windows.Forms ;
using System.IO;
using System.Threading;
using System.Net.Sockets;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net;


namespace WindowsService1
{
/// <summary>
/// Summary description for QuoteServer.
/// </summary>
public class QuoteServer
{
private TcpListener listener;
private int port;
private string filename;
private StringCollection quotes;
private Random random;
private Thread listenerThread;

public QuoteServer() : this ("quotes.txt")
{

}
public QuoteServer(string filename) : this(filename, 7890)
{
}
public QuoteServer(string filename, int port)
{
this.filename = filename;
this.port = port;
}
protected void ReadQuotes()
{
quotes = new StringCollection();
Stream stream = File.OpenRead(filename);
StreamReader streamReader = new StreamReader(stream);
string quote;
while ((quote = streamReader.ReadLine()) != null)
{
quotes.Add(quote);
}
streamReader.Close();
stream.Close();
random = new Random();
}
protected string GetRandomQuoteOfTheDay()
{
int index = random.Next(0, quotes.Count);
return quotes[index];
}
public void Start()
{
ReadQuotes();
listenerThread = new Thread(
new ThreadStart(this.Listener));
listenerThread.Start();
}
protected void Listener()
{
try
{
listener = new TcpListener(port);
listener.Start();
while (true)
{
Socket socket = listener.AcceptSocket();
string message = GetRandomQuoteOfTheDay();
UnicodeEncoding encoder = new UnicodeEncoding();
byte[] buffer = encoder.GetBytes(message);
socket.Send(buffer, buffer.Length, 0);
socket.Close();
}
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
}
public void Stop()
{
listener.Stop();
}
public void Suspend()
{
listenerThread.Suspend();
}
public void Resume()
{
listenerThread.Resume();
}

public void RefreshQuotes()
{
ReadQuotes();
}
static void Main(string[] args)
{
QuoteServer qs = new QuoteServer(@"c:\quotes.txt",4567);
qs.Start();
Console.WriteLine("Hit return to exit");
Console.ReadLine();
// qs.Stop();
}
}

}