Professional Documents
Culture Documents
C#
2Trong lp trnh, Socket l mt API (Application Programming Interface) cung cp cc phng thc giao tip thng qua
mng.
Kt ni Server-Client vi TCP/IP
Khi c chy, server cn c xc nh r a ch IP v s lng nghe trn mt port c th. Server s nm trong trng thi
ny cho n khi client gi n mt yu cu kt ni. Sau khi c server chp nhn, mt connection s hnh thnh cho php
server v client giao tip vi nhau.
C th hn, cc bc tin hnh trn server v client m ta cn thc hin s dng giao thc TCP/IP trong C# (c th chy server
v client trn cng mt my):
Server:
1.
2.
3.
4.
ng Socket.
Thng thng quy trnh ny s c t trong mt vng lp (lp li bc 2) chp nhn nhiu kt ni cng lc (s dng
Thread) hoc cc kt ni ln lt.
Client:
1.
To mt i tng System.Net.Sockets.TcpClient
2.
3.
4.
5.
ng lung v socket.
Tip n cho chy client, nu kt ni thnh cng, server s hin th thm dng thng bo tng t nh sau:
Connection received from 127.0.0.1:2578
Chuyn qua ca s console ca client v nhp tn ca bn vo, nu nhn c d liu, server s gi tr li dng thng ip
Hello [Your Name]
Connected to Y2Server.
Cch s dng Socket Lp trnh Socket giao tip TCP client/server lp trnh Client-Server Lp Trnh Socket Client-server vi Network Stream trong C# LP
TRNH SOCKET VI TCP/UDP Lp trnh C# Lp trnh Source code Source code C Source code .net
i
v
i
11 Answers
active
break;
}
// 3. send
writer.WriteLine("Hello "+str);
}
// 4. close
stream.Close();
socket.Close();
listener.Stop();
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex);
}
Console.Read();
}
}
Y2Client.cs (v2):
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
public class Y2Client{
private const int BUFFER_SIZE=1024;
private const int PORT_NUMBER=9999;
static ASCIIEncoding encoding= new ASCIIEncoding();
public static void Main() {
try {
TcpClient client = new TcpClient();
// 1. connect
client.Connect("127.0.0.1",PORT_NUMBER);
Stream stream = client.GetStream();
Console.WriteLine("Connected to Y2Server.");
while(true)
{
Console.Write("Enter your name: ");
string str = Console.ReadLine();
var reader=new StreamReader(stream);
var writer=new StreamWriter(stream);
writer.AutoFlush=true;
// 2. send
writer.WriteLine(str);
// 3. receive
str=reader.ReadLine();
Console.WriteLine(str);
if(str.ToUpper()=="BYE")
break;
}
// 4. close
stream.Close();
client.Close();
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex);
}
Console.Read();
}
}
Bn chy v d ny ging nh v d u tin v g exit vo client thot ng dng.
tr li 25/11/2014
0Cho Qun!mnh xem v d, nhng khi chy trn hai my khc nhau c kt ni Lan th khng c, cho mnh lm th no
giao tip gia hai my qua mng c?(xin li v mnh ang tm hiu v giao tip mng nn hi b stupid!)
tr li 25/11/2014
Vng Lm
20 0 1
Cho a, u tin cho e xin cm n a dnh thi gian vit nhng bi hit rt b ch
Th 2 a cho e cht :) E ang d nh lm phn mm qun l mail (c th l Gmail) th th e phi bt u t u
tr li 25/11/2014
Long Vn Thnh
20 0 43
Bn c th tm hiu v cc c ch gi nhn mail. Trong .NET th h tr cc API v mail rt y v vic to cng d dng.
Bn c th xem mt vi hng dn trn codeproject s dng ASP.NET nh v d sau:
http://www.codeproject.com/Articles/19609/SMTP-Mail-System-Using-ASP-NET-2-0
tr li 25/11/2014
Long Vn Thnh
20 0 43
Bn i, nu d liu l m thanh v hnh nh th sao , VD nh mun chat voice hay webcam trong mt chng trnh chat
mng LAN y ,bn c th ch cho mnh cch thu v pht cc loi d liu c khng
tr li 25/11/2014
HoanokhongTan
20 0 7
@HoanokhongTan
Cc ng dng ny thng dng giao thc UDP c tc nhanh hn.
tr li 25/11/2014
Anh i, cho em hi nu mun gi v nhn arraylist gia Client v Server dng giao thc TCP th phi lm sao?Cm n
anh trc.^^!
tr li 25/11/2014
Mc Nhi
20 0 6
Phng php gi v nhn i tng l chuyn n thnh dng nh phn (binary). Bn dng hai lp BinaryWriter v
BinaryReader (System.IO) gi v nhn d liu, hoc dng trc tip i tng Socket lm iu ny
V d:
+ Server:
IPAddress address = IPAddress.Parse("127.0.0.1");
TcpListener listener=new TcpListener(address, PORT_NUMBER);
listener.Start();
Socket socket=listener.AcceptSocket();
var bytes = ObjectToByteArray(obj);
socket.Send(bytes);
+ Client:
Socket soc =
new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
soc.Connect("127.0.0.1", PORT_NUMBER);
byte[] buffer = new byte[1000];
soc.Receive(buffer);
var obj = ByteArrayToObject(buffer);
Hai phng thc ObjectToByteArray() v ByteArrayToObject(), bn c th chm ti y: ^_^
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Byte-array-to-object.html
v
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Object-to-byte-array.html
http://hoidapit.com.vn/Questions/ViewQuestions/3137/huong-dan-lap-trinh-socketgiao-tiep-tcp-client-server-trong-c.html