TCP- GUI NHẬN BẰNG STRING
SERVER NHÉ CHÓ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace server
{
class Program
{
static bool kttang(int[] a, int n)
{
for (int i = 0; i < n-1; i++)
{
if (a[i] > a[i + 1])
return false;
}
return true;
}
static bool xuathien(int[] a, int n, int x) {
for (int i = 0; i < n; i++)
if (a[i] == x)
return true;
return false;
}
static bool songuyento(int[] a, int n) {
if (n <= 1)
return false;
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
static bool sochinhphuong(int a, int n) {
if (Math.Sqrt(a) * Math.Sqrt(a) == a) return true;
return false;
}
Trang 2static int max(int[] a, int n)
{
int max = 0;
for (int i = 0; i < n; i++)
{
if (a[i] > max)
max = a[i];
}
return max;
}
static int min(int[] a, int n)
{
int min = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] < min)
min = a[i];
}
return min;
}
static void Main(string[] args)
{
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2018);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(ipe);
server.Listen(10);
Console.WriteLine("Waitting ");
Socket client = server.Accept();
Console.WriteLine("Chap nhan ket noi tu: " + client.RemoteEndPoint.ToString()); string s = "Server chao ban!";
NetworkStream ns = new NetworkStream(client);
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
// sw.WriteLine(s_gui);
sw.WriteLine(s);
sw.Flush();
while (true)
{
s= sr.ReadLine();
if (s == "exit") break;
Console.WriteLine("Client: " + s);
while (s.IndexOf(" ") != -1)
s = s.Replace(" ", " ");
s=s.Trim();
string[] str = s.Split(' ');
int n = str.Length;
int[] a = new int[100];
try
{
for (int i = 0; i < str.Length; i++)
{
a[i] = int.Parse(str[i]);
}
}
catch
{
sw.WriteLine("sai dinh dang");
sw.Flush();
}
string s5;
if (kttang(a, n))
Trang 3s5="server: chuoi vua nhap tang dan";
else s5="server: chuoi vua nhap khong tang dan"; sw.WriteLine(s5);
sw.Flush();
int x = int.Parse(sr.ReadLine()); //string s2 = " "; string s4; if (xuathien(a, n, x)) {
s4 = "cac vi tri xuat hien cua " + x.ToString() + " la:";
for (int i = 0; i < n; i++) {
if (a[i] == x) {
s4 = s4 + (i + 1).ToString() + " "; }
}
}
else s4 = "khong xuat hien trong day."; sw.WriteLine(s4); sw.Flush(); //Console.ReadKey(); //string s3 = " "; s4 = "cac so chinh phuong trong mang la:"; for (int i = 0; i < n; i++) if (sochinhphuong(a[i], n)) {
s4 = s4 + a[i].ToString() + " "; }
sw.WriteLine(s4); sw.Flush(); //string s4 = " "; s4 = "so lon nhat trong day:" + max(a, n).ToString(); sw.WriteLine(s4); sw.Flush(); //string s4 = " "; s4 = "so nho nhat trong day:" + min(a, n).ToString(); sw.WriteLine(s4); sw.Flush(); for (int i = 0; i < n - 1; i++) {
if (a[i] % 2 != 0) {
for (int j = i + 1; j < n; j++) {
if (a[j] % 2 != 0) {
if (a[i] >= a[j]) {
int tg = a[i]; a[i] = a[j]; a[j] = tg; }
}
}
}
Trang 4}
s4 = "chuoi sap xep le tang dan la: ";
for (int i = 0; i < n; i++)
{
s4 = s4 + a[i].ToString() + " ";
}
sw.WriteLine(s4);
sw.Flush();
}
ns.Close();
sw.Close();
sr.Close();
client.Close();
server.Close();
}
}
}
CLIENT NHÉ CHÓ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace client
{
class Program
{
static void Main(string[] args)
{
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2018); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipe);
NetworkStream ns = new NetworkStream(client);
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
while (true)
{
//Nhan mess tu server
//byte[] data_nhan = new byte[1024];
//int dembyte = client.Receive(data_nhan);
//String s = Encoding.ASCII.GetString(data_nhan, 0, dembyte);
String s = sr.ReadLine();
//String s = sr.ReadLine();
Console.WriteLine("Server: "+s);
//Gui mess sang Server
s = Console.ReadLine();
sw.WriteLine(s);
sw.Flush();
//byte[] data_gui = new byte[1024];
Trang 5//data_gui = Encoding.ASCII.GetBytes(s);
//client.Send(data_gui, data_gui.Length, SocketFlags.None);
if (s == "exit") break;
string s5 = sr.ReadLine();
Console.WriteLine(" " + s5);
s = Console.ReadLine();
sw.WriteLine(s);
sw.Flush();
string s4 = sr.ReadLine();
Console.WriteLine(" " + s4);
s4 = sr.ReadLine();
Console.WriteLine(" " + s4);
s4 = sr.ReadLine();
Console.WriteLine(" " + s4);
s4 = sr.ReadLine();
Console.WriteLine(" " + s4);
s4 = sr.ReadLine();
Console.WriteLine(" " + s4);
}
client.Dispose();
//ns.Close();
//sw.Close();
//sr.Close();
client.Close();
}
}
}
UDP- GỦI NHẬN THÔNG QUA BYTE
SERVER NHÉ CHÓ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace server
{
class Program
{
static bool kttang(int []a,int n)
{
for(int i=0;i<n-1;i++)
Trang 6{
if (a[i] > a[i + 1])
return false ;
}
return true;
}
static bool xuathien(int []a,int x,int n)
{
for (int i = 0; i < n; i++)
{
if (a[i] ==x)
return true;
}
return false;
}
static bool snt(int n)
{
for (int i = 2; i < n; i++)
{
if (n%i==0)
return false ;
}
return true;
}
static int max(int []a,int n)
{
int max=a[0];
for (int i = 0; i < n; i++)
{
if (a[i] >= max)
max = a[i];
}
return max;
}
static void Main(string[] args)
{
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2019);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
server.Bind(ipe);
IPEndPoint ipenew = new IPEndPoint(IPAddress.Any, 0);
EndPoint ep = (EndPoint)ipenew;
//nhan
byte[] nhan = new byte[1024];
int demnhan = server.ReceiveFrom(nhan, ref ep);
string snhan = Encoding.ASCII.GetString(nhan, 0, demnhan);
Console.WriteLine("client :" + snhan);
//gui
byte[] gui = new byte[1024];
string sgui = "chao client, t la server";
gui = Encoding.ASCII.GetBytes(sgui);
server.SendTo(gui, ep);
while (true )
{
nhan = new byte[1024];
demnhan = server.ReceiveFrom(nhan, ref ep);
snhan = Encoding.ASCII.GetString(nhan, 0, demnhan);
while (snhan.IndexOf(" ") != -1)
snhan = snhan.Replace(" ", " ");
Trang 7snhan.Trim();
string[] str = snhan.Split(' ');
int n = str.Length;
int[] a = new int[n];
for (int i = 0; i < str.Length; i++)
{
a[i] = int.Parse(str[i]);
}
string kq;
if(kttang (a,n))
kq="day duoc sap xep";
else kq="day chua duoc sap xep";
gui = new byte[1024];
gui = Encoding.ASCII.GetBytes(kq);
server.SendTo(gui, ep);
//gui x
nhan = new byte[1024];
demnhan = server.ReceiveFrom(nhan , ref ep);
snhan = Encoding.ASCII.GetString(nhan, 0, demnhan); int x = int.Parse(snhan);
if (xuathien(a, x, n))
{
kq = "co xuat hien " + x.ToString() + " o vi tri : "; for (int i = 0; i < n; i++)
{
if (a[i] == x)
{
kq = kq + (i + 1).ToString() + " ";
}
}
}
else
kq = "khong xuat hien";
gui = new byte[1024];
gui = Encoding.ASCII.GetBytes(kq);
server.SendTo(gui, ep);
kq = "so lon nhat: " + max(a, n).ToString();
gui = new byte[1024];
gui = Encoding.ASCII.GetBytes(kq);
server.SendTo(gui, ep);
kq = "cso nguyen to la : ";
for (int i = 0; i < n; i++)
{
if (snt (a[i]))
{
kq = kq + a[i].ToString () + " ";
}
}
kq = "mang sau sap xep la:";
for(int i=0;i<n-1;i++)
{
if(a[i]%2!=0)
for(int j=i+1;j<n;j++)
{
if (a[j] % 2 != 0)
Trang 8{
if(a[i]>=a[j]) {
int tam = a[i]; a[i] = a[j]; a[j] = tam; }
}
}
}
for(int i=0;i<n;i++) {
kq = kq + a[i].ToString() + " "; }
gui = new byte[1024]; gui = Encoding.ASCII.GetBytes(kq); server.SendTo(gui, ep);
}
server.Close();
}
}
} CLIENT NHÉ CHÓ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; using System.IO; namespace clinet { class Program {
static void Main(string[] args) {
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2019); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); EndPoint ep = (EndPoint)ipe; byte [] gui = new byte[1024]; string sgui = "chao server, client day"; gui = Encoding.ASCII.GetBytes(sgui); client.SendTo(gui, ep); byte[] nhan = new byte[1024]; int demnhan = client.ReceiveFrom(nhan, ref ep); string snhan = Encoding.ASCII.GetString(nhan ,0 ,demnhan ); Console.WriteLine("server :" + snhan); while (true ) {
t: gui = new byte[1024];
sgui = Console.ReadLine();
while (sgui.IndexOf(" ") != -1)
sgui = sgui.Replace(" ", " ");
Trang 9sgui.Trim();
string[] str = sgui.Split(' ');
int n = str.Length;
int[] a = new int[n];
try
{
for(int i=0;i<str.Length ;i++)
{
a[i] = int.Parse(str[i]);
}
}
catch
{
Console.WriteLine("sai dinh dang nhap lai:");
goto t;
}
gui = Encoding.ASCII.GetBytes(sgui);
client.SendTo(gui, ep);
nhan = new byte[1024];
demnhan = client.ReceiveFrom(nhan, ref ep);
string kq= Encoding.ASCII.GetString(nhan, 0, demnhan); Console.WriteLine("server :" + kq);
//gui x
gui = new byte[1024];
sgui = Console.ReadLine();
gui = Encoding.ASCII.GetBytes(sgui);
client.SendTo(gui, ep);
nhan = new byte[1024];
demnhan = client.ReceiveFrom(nhan, ref ep);
kq = Encoding.ASCII.GetString(nhan, 0, demnhan);
Console.WriteLine("server :" + kq);
nhan = new byte[1024];
demnhan = client.ReceiveFrom(nhan, ref ep);
kq = Encoding.ASCII.GetString(nhan, 0, demnhan); Console.WriteLine("server :" + kq);
nhan = new byte[1024];
demnhan = client.ReceiveFrom(nhan, ref ep);
kq = Encoding.ASCII.GetString(nhan, 0, demnhan); Console.WriteLine("server :" + kq);
nhan = new byte[1024];
demnhan = client.ReceiveFrom(nhan, ref ep);
kq = Encoding.ASCII.GetString(nhan, 0, demnhan); Console.WriteLine("server :" + kq);
}
client.Close();
}
}
}