class Client{ static void Mainstring[] args { IPEndPoint iep = new IPEndPointIPAddress.Parse"127.0.0.1", 2008; Socket client = new SocketAddressFamily.InterNetwork, SocketType.Stream
Trang 1class Client
{
static void Main(string[] args)
{
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2008);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(iep);
byte[] data = new byte[1024];
int recv = client.Receive(data);
string s = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("Server gui:{0}", s);
string input;
while (true)
{
input = Console.ReadLine();
//Chuyen input thanh mang byte gui len cho server
data = new byte[1024];
data = Encoding.ASCII.GetBytes(input);
client.Send(data, data.Length, SocketFlags.None);
if (input.ToUpper().Equals("QUIT")) break;
data = new byte[1024];
recv = client.Receive(data);
s = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("Server gui:{0}", s);
if (s == "QUIT") break;
}
client.Disconnect(true);
client.Close();
cau 2 server
class Seryer
{
static void Main(string[] args)
{
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2008);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Bind(iep);
server.Listen(10);
Console.WriteLine("Cho ket noi tu client");
Socket client = server.Accept();
Console.WriteLine("Chap nhan ket noi tu:{0}", client.RemoteEndPoint.ToString());
string s = "CMung DVoi Server, Upper, Tinh Tong voi CP: T X Y, T.DO X la ST1, Y ST2"; //Chuyen chuoi s thanh mang byte
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(s);
//gui nhan du lieu theo giao thuc da thiet ke
client.Send(data, data.Length, SocketFlags.None);
while (true)
{
data = new byte[1024];
int recv = client.Receive(data);
if (recv == 0) break;
//Chuyen mang byte Data thanh chuoi va in ra man hinh
s = Encoding.ASCII.GetString(data, 0, recv);
//Bat dau tinh Tong, Xem Cu Phap Nhap Tren
string[] ma = new string[5];
int t=0;
Trang 2for (int i = 0; i < 5; i++)
{
ma = s.Split(' ');
}
try//xu li ngoai le
{
if (ma[0] == "T")
{
t = Convert.ToInt32(ma[1]) + Convert.ToInt32(ma[2]); }
}
catch {
string gcl="Ban Da Nhap Sai Cu Phap, Enter De Ket Thuc CT"; data = Encoding.ASCII.GetBytes(gcl);
client.Send(data, data.Length, SocketFlags.None);
s="QUIT";
data = Encoding.ASCII.GetBytes(s);
client.Send(data, data.Length, SocketFlags.None);
}
Console.WriteLine(t.ToString());
Console.WriteLine("Clien gui len:{0}", s);
//Neu chuoi nhan duoc la Quit thi thoat
if (s.ToUpper().Equals("QUIT")) break;
//Gui tra lai cho client chuoi s
s = s.ToUpper();
data = new byte[1024];
//Tai day dung if de lua chon upper hay tinh tong
if (ma[0] == "T")
{
Data=Encoding.ASCII.GetBytes(t.ToString());
}
else {
data = Encoding.ASCII.GetBytes(s);
}
client.Send(data, data.Length, SocketFlags.None);
}
client.Shutdown(SocketShutdown.Both);
client.Close();
server.Close();
cau 3 xu li chuoi
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Nhap vao chuoi 1");
string s = Console.ReadLine();
Console.WriteLine("\nNhap vao chuoi 2");
string s2 = Console.ReadLine();
string[]b=s.Split(' ');
string c=" ";
for(int i=0;i<b.Length;i++)
{
if(!b[i].Equals(" "))
Trang 3c+=b[i];
}
}
string[] t = s2.Split(' ');
string w = " ";
for (int i = 0; i <t.Length; i++)
{
if (!t[i].Equals(" "))
{
w += t[i];
}
}
Console.WriteLine("\nMang sau khi xoa khoang trang la : {0}",c); char[]d=s.ToCharArray();
Array.Reverse(d);
Console.WriteLine("\nDao nguoc chuoi");
Console.WriteLine(d);
Array.Sort(b);
Console.WriteLine("\nSap xep chuoi theo do dai tang dan"); foreach (string tang in b)
{
Console.WriteLine(tang);
}
Console.WriteLine("\nSap xep chuoi theo do dai giam dan"); Array.Reverse(b);
foreach (string giam in b)
{
Console.WriteLine(giam);
}
Console.Write("\nXac dinh vi tri can tim trong chuoi\n");
Console.WriteLine("Nhap ki tu muon tim chuoi 1");
char m =char.Parse(Console.ReadLine());
int z = s.IndexOf(m);
Console.WriteLine("Vi tri ki tu trong chuoi 1 la: {0}\n",z+1); Console.WriteLine("Nhap ki tu muon tim chuoi 2");
char m1 = char.Parse(Console.ReadLine());
int z1 = s2.IndexOf(m1);
Console.WriteLine("Vi tri ki tu trong chuoi 2 la: {0}\n", z1+1); Console.Write("\nGhep chuoi\n");
``Console.WriteLine("Ghep chuoi 1 vao chuoi 2");
string[] mang = new string[] { s, s2 };
string q = String.Join(" ", mang);
Console.WriteLine(q);
Console.Write("\nSo sanh chuoi\n");
int y = String.Compare(s, s2);
if(y==1)
Console.Write("s ngan hon s2\n");
else if(y==-1)
Console.Write("s dai hon s2\n");
else
Console.Write("s bang s2\n");
Console.Write("\nChen phan tu vao vi tri xac dinh trong chuoi s\n"); Console.Write("Nhap tu ban muon chen: ");
string k = Console.ReadLine();
int a;
Trang 4Console.Write("Nhap vi tri ban muon chen trong chuoi s a = ");
a = int.Parse(Console.ReadLine());
string chen = s.Insert(a, k);
Console.Write("Ket qua sau khi chen: " + chen);
Console.Write("\n Xoa phan tu trong chuoi s\n");
int a1,b1;
Console.Write("Nhap vi tri bat dau xoa trong chuoi s a1 = "); a1 = int.Parse(Console.ReadLine());
Console.Write("Xoa bao nhieu phan tu trong chuoi s b1 = "); b1 = int.Parse(Console.ReadLine());
string xoa = s.Remove(a1, b1);
Console.Write("Ket qua sau khi xoa: " + xoa);
Console.Write("\n Thay the tu bat ki trong chuoi s\n");
string a2,b2;
Console.Write("Nhap tu muon thay the: ");
a2=Console.ReadLine();
Console.Write("Thay bang: ");
b2=Console.ReadLine();
string thaythe = s.Replace(a2, b2);
Console.Write("Ket qua sau khi thay the s = " + thaythe); Console.ReadLine();
}
}
}
Cau 4 xay dung lop sv
class sinhvien
{
public string tensv;
public string ngaysinh;
public float diem;
public sinhvien()
{
tensv = "nguyen van a";
ngaysinh = "08/12/89";
diem = 0;
}
public sinhvien(string t, string ns, float d)
{
tensv = t;
ngaysinh = ns;
diem = d;
}
public void nhap()
{
Console.WriteLine("ban hay nhap ten cho sv:"); tensv = Console.ReadLine();
Console.WriteLine("ban hay nhap ngay sinh cho sv:"); ngaysinh = Console.ReadLine();
Console.WriteLine("ban hay nhap diem cho sv:"); diem= float.Parse(Console.ReadLine());
}
class Program
{
static void Main(string[] args)
{
int n;
Trang 5Console.WriteLine("ban muon nhap bao nhieu sv:");
n= int.Parse(Console.ReadLine());
sinhvien[] sv = new sinhvien[n];
for (int i = 0; i< n;i++)
{
sv[i] = new sinhvien();
Console.WriteLine("nhap thong tin sinh vien:");
sv[i].nhap();
cau 5 xu li file
string fn = "filebinary.txt";
FileStream myfile = new FileStream(fn, FileMode.CreateNew); BinaryWriter brfile = new BinaryWriter(myfile);
int n;
Console.WriteLine(" Nhap N So Nguyen");
n = Int32.Parse(Console.ReadLine());
for (int i = 0; i <= n; i++)
{
brfile.Write(i);
}
brfile.Close();
myfile.Close();
cau 6 tinh tong fil nhi phan
string fn = "C:/filebinary.txt";
FileStream myfile = new FileStream(fn, FileMode.Open); BinaryReader brfile = new BinaryReader(myfile);
int a,tong=0;
Console.WriteLine("Bat dau doc File ");
while(brfile.PeekChar()!=-1)
{
a= brfile.ReadInt32();
Console.Write("{0}", a);
tong = tong + a;
}
Console.WriteLine();
Console.WriteLine("Ket thuc qua trinh doc File ");
Console.WriteLine(" Tong = {0}", tong);
Console.ReadLine();
brfile.Close();
cau 7 mang voi forch
int []a = new int[5];
Console.WriteLine("Nhap vao cac phan tu ");
for(int i =0; i<5; i++)
{
Console.Write("Phan tu a[{0}]= ",i);
a[i] = int.Parse(Console.ReadLine());
}
int tong = 0;
for(int i = 0; i < a.Length; i++)
tong =tong +a[i];
///////Xuat mang
Console.WriteLine("Danh sach cac phan tu ");
foreach (int i in a)
{
Console.WriteLine(i.ToString()+"\t");
Trang 6}
Console.WriteLine(" Tong cac phan tu = {0}",tong); Console.Read();
}
Cau 8 mang so nguyen
int[] n;
n = new int[100];
int m;
int s = 0;
Console.WriteLine("ban muon nhap bao nhieu phan tu:");
m = int.Parse(Console.ReadLine());
for (int i = 0; i < m; i++)
{
Console.WriteLine("ban hay nhap phan tu thu a[{0}]:", i); n[i] = int.Parse(Console.ReadLine());
if (n[i] % 5 == 0)
s = s + 1;
}
Console.WriteLine("so luong cac so chia het cho 5 la:{0}", s); Console.ReadLine();
}