Server side: server.cpp // ---------------------------------------------------------------------------------------------------------------------------------- ---------------------------------
#include
#include "unit1.h" // --------------------------------------- ---------------------------------- # Pragma package (smart_init) #pragma resource "* .dfm" TFORM1 * Form1; Ansistring Nowlogname = ""; ANSISTRING NOWLOGHOST = ""; Ansistring Nowlogaddress = ""; // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------__ fastcall tform1: : TFORM1 (TComponent * Owner: TFORM (Owner) {} // ---------------------------------- -----------------------------------------
Void __fastcall tform1 :: formc (TOBJECT * Sender) {memo1-> clear (); listbox1-> clear (); listbox2-> clear (); listbox3-> clear (); serversocket1-> active = true;} // -------------------------------------------------- -------------------------
Void __fastcall tform1 :: formd * sender {memo1-> clear (); listbox1-> clear (); listbox2-> clear (); listbox3-> clear (); ansistring sysinfo; sysinfo = "[" Timetostr (NOW ()) "]" "with the server lost connection! Please reconnect!"; Int m = 0; m = serversocket1-> socket-> activeConnections; for (int i = 0; i
} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- void __fastcall tform1 :: servet1clientread (Tobject * sender, tcustomwinsocket * socket) {nowLoghost = Socket-> Remotehost ;Logaddress = socket-> remoteaddress; Ansistring Data = socket-> receiveText (); // Receive user sending information ANSISTRING SUBDATA1 = DATA.SUBSTRING (Data.Length () - 7, 8); // Analysis Information ANSISTRING SUBDATA2 = Data.substring (1, data.length () - 8); if (Subdata1 == "@ login @@") // If it is the user login information {listbox1-> items-> add (subdata2); // user Information Add to User List ListBox2-> Items-> Add (nowLogAddress); listbox3-> items-> add (nowloghost); Ansistring sysinfo = "[" Timetostr (now ()) "]" Subdata2 "joined a meeting ! "; // Display system information MEMO1-> lines-> add (sysinfo); if (Serversocket1-> Socket-> ActiveConnections) for (int i = 0; i
} Else if (subdata1 == "@ logout @") // If the user exits the meeting {int index1 = listbox1-> items-> indexof (subData2); // Remove the user information from the user list ListBox1-> items- > Delete (Index1); int Index2 = ListBox2-> Items-> IndexOf (NowlogAddress); ListBox2-> Items-> Delete (Index2); int Index3 = ListBox3-> Items-> IndexOf (NowlogHost); ListBox3-> Items- > Delete (index3); memo1-> lines-> add ("[" Timetostr (now ()) "]" Subdata2 "exits the meeting!"); // Display System Information IF (ServerSocket1-> Socket- > ActiveConnections) for (int i = 0; i
#include
#include "unit1.h" #include "iostream.h" #include "io.h" #include "time.h" #include "stdio.h" #include "alloc.h" // -------- -------------------------------------------------- ------------------- # prgma package (smart_init) #pragma resource "* .dfm" TFORM1 * FORM1; ANSISTRING YONAME = "YourName"; Ansistring IP = "255.255. 255.0 "; ANSISTRING HOSTNAME =" "; ANSISTRING NOWLOGNAME =" "; Ansistring Nowloghost ="; Ansistring Nowlogaddress = ""; // --------------------- -------------------------------------------------- ----__ fastcall tform1 :: tform1 (tComponent * Owner: TFORM (OWNER) {} // --------------------------- ------------------------------------------------
Void __fastcall tform1 :: formcall (tobject * sender) {memo1-> clear (); memo2-> clear (); memo3-> clear (); memo4-> clear (); bitbtn6-> enabled = false; edit1-> Text = ""; edit2-> text = "dy-02"; statictext1-> caption = ""; statictext5-> caption = ""; statictext6-> caption = ""; staticText7-> caption = "; panel1- > Caption = "private chat object:"; // -------------------------------------------------------------------------------------------------------------------------------------------- - // Get local IP addresses and hostnames Word WVersionRequested; Wsadata Wsadata; // Start Up Winsock WversionRequested = MakeWord (1, 1); WSAStartup (WVersionRequested, & WSADATA);
Hostent * p; char s [128]; char * p2; // get the computer name gethostname (s, 128); p = gethostByName (s); statictext5-> caption = p-> h_name; // Get the ipaddress P2 = INET_NTOA (* (in_addr *) P-> h_addr)); staticText6-> caption = p2;} // ------------------------ -------------------------------------------------- -void __fastcall tform1 :: bitbtn4click (TOBJECT * Sender) {memo3-> clear ();} // ------------------------- ---------------------------------------------- void __fastcall TFORM1 :: Bitbtn2Click (TOBJECT * Sender) {memo1-> clear ();} // ------------------------------ ------------------------------------------- // Connect the server void __fastcall TForm1 :: ClientSocket1Connect (TObject * Sender, TCustomWinSocket * Socket) {StatusBar1-> SimpleText = "successfully connected to the server" ClientSocket1-> Host ;; ClientSocket1-> Socket-> SendText (yourName "@ login @@"); StaticText7-> CAPTION = DATETOSTR (now ()) " Time ();} // ---------------------------------------------------------------------------------------- -----------------------------------------------//send Information VOID __FASTCALL TFORM1 :: Bitbtn1click (Tobject * sender) {f (Memo2- > Lines-> text == "") Application-> MessageBox ("error, no information! "," Tips ", MB_OK); Else {ANSISTRING DATA; DATA = Yourname ": " Memo2-> Lines-> text; clientsocket1-> socket-> sendtext (data); // memo1-> lines-> add ( Data); Memo2-> lines-> clear (); memo2-> setfocus ();}} // ---------------------------------------------------------------------------------------------------------------- ------------------------------------- / / Receive the information sent by the server VOID __FASTCALL TFORM1 :: ClientSocket1read (Tobject * Snder, Tcustomwinsocket * Socket) {INT i = 0; ANSISTRING CH = "; NowlogName =" "; Nowloghost ="
NowlogAddress = ""; Ansistring Data = Socket-> ReceiveText (); // Receive the information sent by the user ANSISTRING SUBDATA1 = DATA.SUBSTRING (1, 8); // Analyze Information IF (Subdata1 == "@ login @@") // If the user logs in {for (i = 9; (ch = DATA.SUBSTRING (i, 1))! = "@"; I ) nowlogname = ch; for (; (ch = data.substring (i 1, 1))! = "@"; I ) Nowloghost = Ch; NowLogAddress = data.substring (i 1, 18); listbox1-> items-> add (nowlogname); // User information Add to User list Listbox2-> items-> add (nowlogaddress); listbox3-> items-> add (nowloghost); Ansistring sysinfo = "[" Timetostr (now ()) "]" NOWLOGNAME "joined a meeting!"; // Display system information MEMO1-> lines-> add (sysinfo);} else1 == "@ logout @") // If the user exits the meeting {for (i = 9; (ch = data.substring (i, 1))! = "@"; I ) nowlogname = ch; for (; (ch = data.substring (i 1, 1))! = "@"; I ) nowloghost = ch; NowLogAddress = data.substring i 1, 18); int index1 = listbox1-> items-> indexof (nowlogname); // Remove the user information from the user list ListBox1-> items-> delete (index1); int index2 = listbox2-> items- > Indexof (nowLogaddress); listbox2-> items-> delete (index2); int index3 = listbox3-> items-> i Ndexof (nowLoghost); listbox3-> items-> delete (index3); memo1-> lines-> add ("[" Timetostr (now ()) "]" NOWLOGNAME "exits a meeting!"); // Display system information} else memo1-> lines-> add (data);
} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- // Private chat space or transfer file selected object void __fastcall tform1 :: listbox1click (Tobject * sender) {if (ClientSocket2-> Active) {ClientSocket2-> Active = false; ClientSocket2-> Close ();} int Index = ListBox1-> ItemIndex; IP = ListBox2-> Items-> Strings [Index]; HostName = ListBox3-> Items-> strings [index]; panel1-> caption = "private chat object:" listbox1-> items-> strings [index]; statictext1-> caption = listbox1-> items-> strings [index]; clientsocket2-> Host = hostname; clientsocket2-> address = ip; clientsocket2-> port = 2222; clientsocket2-> active = true; bitbtn3-> enabled = true;
} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- // Send information on the private chat space to the selected user void __fastcall tform1 :: bitbtn3click (TOBJECT * SENDER ) {IF (Memo4-> Lines-> text == ") Application-> MessageBox (" error, unacahyd message! "," Tips ", MB_OK); Else {ANSISTRING DATA; DATA = YourName " Pair " StaticText1-> CAPTION "said:" Memo4-> lines-> text; clientsocket2-> socket-> sendtext (data); // memo3-> lines-> add (data); memo4-> lines-> clear () Memo4-> setfocus ();}} // -------------------------------------- ------------------------------------- // Select the file you want to transfer void __fastcall tform1 :: bitbtn5click (TOBJECT * Sender) {if (nmstrm1-> connect) nmstrm1-> disconnect (); if (OpenDialog1-> execute ()) {edit1-> text = OpenDialog1-> filename; bitbtn6-> enabled = true;}} / / --------------------------------------------------- -------------------------- // Log in to the server void __fastcall tform1 :: button1click (Tobject * sender) {if (clientsocket1-> active) {ClientSocket1-> Active = false; clientsocket1-> close ();} listbox1-> items-> clear (); listbox2-> items-> CLEAR (); memo1-> clear (); memo3-> clear (); if ("Loger", "Please enter your nickname:", YourName) // Enter nickname {statusbar1-> simpletext = "Connecting Server ..."; ClientSocket1-> address = maskedit1-> usxt; clientsocket1-> host = edit2-> text; clientsocket1-> port = 4000; clientsocket1-> active = true; bitbtn1-> enabled = true; Bitbtn3-> enabled = true;}} // --------------------------------------- ------------------------------------
Void __fastcall tform1 :: butt * sender {listbox1-> items-> clear (); listbox2-> items-> clear (); listbox3-> items-> clear (); clientsocket1-> active = false; statusbar1 -> SimpleText = "Connection Interrupt ...";} // ----------------------------------- ---------------------------------------- Void __fastcall tform1 :: formstroy (TOBJECT * SENDER ) {Wsacleanup (); serversocket1-> close (); clientsocket1-> close (); clientsocket2-> close (); nmstrm1-> cancel (); nmstrMserv1-> Cancel (); close ();} // -------------------------------------------------- -----------------------
Void __fastcall tform1 :: n2click (Tobject * sender) {tabbednotebook1-> pageindex = 3; statusbar1-> simpletext = "Transfer file ..."; edit1-> setfocus ();} // -------- -------------------------------------------------- -----------------
Void __fastcall tform1 :: n1click (Tobject * sender) {tabbednoteBook1-> pageIndex = 2; statusbar1-> simpletext = "private chat space ..."; memo4-> setfocus ();} // ------- -------------------------------------------------- ------------------ Void __fastcall tform1 :: tabednotebook1change (Tobject * Sender, int newtab, bool& allowchange) {if (newtab == 0) statusbar1-> simpletext = "connection Setting ... "; else if (newtab == 1) statusbar1-> simpletext =" network conference ... "; else if (newTab == 2) statusbar1-> simpletext =" private chat space ... "; else IF (newTab == 3) statusbar1-> simpletext = "Transfer file ..."; else if (newTab == 4) statusbar1-> simpletext = "Normal Information ..."; else if (newTab == 5) STATUSBAR1-> SimpleText = "Version information ...";} // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------- void __fastcall tform1 :: button3click (TOBJECT * Sender) {listbox1-> items-> clear (); listbox2-> items-> clear (); listbox3-> items-> clear (); clientsocket1-> active = false; statusbar1-> simpletext = "connection has been interrupted. .. "; Close ();} // --------------------------------------- ------------------------------------ Void __fastcall tform1 :: serversocket1clientRead (Tobject * sender, tcustomwinsocket * socket) {ANSISTRING DATA = Socket-> ReceiveText (); // Receive information of private chat objects Memo3-> lines-> add (data);} // -------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------
void __fastcall TForm1 :: ClientSocket1Disconnect (TObject * Sender, TCustomWinSocket * Socket) {ClientSocket1-> Socket-> SendText (yourName "@ logout @"); ClientSocket1-> Close ();} // -------- -------------------------------------------------- ----------------- Void __fastcall tform1 :: clientsocket1error (Tobject * Snder, Tcustomwinsocket * Socket, TerRorevent ErrorEvent, Int & ErrorCode) {Application-> MessageBox ("Unable to connect host! No or the server is not started! " -------------------------------------------------- ----------------
Void __fastcall tform1 :: clientsocket2connect (Tobject * sender, tcustomwinsocket * socket) {statusbar1-> simpletext = "successfully connected" staticText1-> caption;} // ------------- -------------------------------------------------- ------------
Void __fastcall tform1 :: clientsocket2disconnect (Tobject * Sender, tcustomwinsocket * socket) {ClientSocket2-> close ();} // ---------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -
void __fastcall TForm1 :: ClientSocket2Error (TObject * Sender, TCustomWinSocket * Socket, TErrorEvent ErrorEvent, int & ErrorCode) {Application-> MessageBox ( "Unable to connect to objects! No Network or the other party is not online!", "Connection failed!", MB_OK ); Statusbar1-> simpletext = "Connection object failed ...";
} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------
Void __fastcall tform1 :: bitbtn6click (tobject * sender) {if (edit1-> text == ") {Application-> MessageBox (" Please select the file you want to transfer! "," ... ", mb_ok); bitbtn6- > Enabled = false;} else {BitBtn6-> Enabled = true; TFileStream * MyFStream; MyFStream = new TFileStream (OpenDialog1-> FileName, fmOpenRead); try {NMStrm1-> Host = HostName; NMStrm1-> FromName = Edit1-> Text NMSTRM1-> postit (myfstream);} catch (...) {} myfstream-> free ();}} // --------------------- -------------------------------------------------- ---- // Receive and save the sent file void __fastcall tform1 :: nmstrMserv1msg (tComponent * sender, const anstring sfrom, tstream * Strm) {ANSISTRING NMFILENAMEXT = ""; int count = 1; for (count = 1; Count <= sfrom.length ();) {IF (Sfrom.Substring (count, 1) == ".") Break; Else Count ;} nmfilenameext = sfrom.Substring (count 1, sfrom.length () - count () ); Savedialog1-> title = "Save File to:"; // Setting the title Savedialog1-> initialdir = "// data"; // set the default path for the dialog savedialog1-> filter = "files (* " NMFileNameext ") | *. " NMFilena Meext; // Set file filter Savedialog1-> default; // Set the default file name if (Savedialog1-> execute ()) {// statusbar1-> simpletext = "is receiving the file:" sfrom ".. . "; TFileStream * MyFStream; MyFStream = new TFileStream (SaveDialog1-> FileName, fmCreate); try {MyFStream-> CopyFrom (strm, strm-> Size);} catch (...) {} MyFStream-> Free () }
if (! NMStrmServ1-> BytesRecvd = NMStrmServ1-> BytesTotal) ProgressBar1-> Position = (float (NMStrmServ1-> BytesRecvd) / float (NMStrmServ1-> BytesTotal)) * 100; else {ProgressBar1-> Position = 100; // Application -> MessageBox ("Receive File End!", "Tips", MB_OK); ProgressBar1-> Position = 0; Statusbar1-> SimpleText = "Receive file end!";}} // --------- -------------------------------------------------- ---------------- void __fastcall tform1 :: bitbtn7click (tobject * sender) {nmstrm1-> can (); nmstrmserv1-> cancel (); statusbar1-> simpletext = "transfer file ... ";} // ------------------------------------------ --------------------------------- void __fastcall tform1 :: nmstrm1invalidhost (Bool & Handled) {Application-> MessageBox (" Unrecognized hosts! "," ", Mb_ok);} // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------- void __fastcall tform1 :: nmstrm1connectionfailed (TOBJECT * Sender) {Application-> MessageBox ("connection failed, please confirm if the other party is correct!", "", Mb_ok);} // -------------------- -------------------------------------------------- ----- void __fastcall tform1 :: nms TRM1MESSAGESENT (TOBJECT * Sender) {if (nmstrm1-> bytessent! = nmstrm1-> bytestotal) statusbar1-> simpletext = "is sending files:" Edit1-> Text "..."; else statusbar1-> SimpleText = "Transfer The file is over! "} // --------------------------------------------- ------------------------------
void __fastcall TForm1 :: NMStrm1Status (TComponent * Sender, AnsiString Status) {StatusBar1-> SimpleText = "Last WinSock error:" IntToStr (NMStrm1-> LastErrorNo); if (NMStrm1-> BeenCanceled) StatusBar1-> SimpleText = "operation is Cancel ... "; if (nmstrm1-> beentimedout) statusbar1-> simpletext =" Operation timeout ... ";} // --------------------- -------------------------------------------------- ---- Void __fastcall tform1 :: nmstrm1connect (TOBJECT * Sender) {statusbar1-> SimpleText = "Connection success ...";} //---------------- -------------------------------------------------- -------
Void __fastcall tform1 :: nmstrm1disconnect (TOBJECT * Sender) {// statusbar1-> SimpleText = "Interrupt connection ...";} // -------------------- -------------------------------------------------- -----
Void __fastcall tform1 :: nmstrm1hostresolved (tcomponent * sender) {statusbar1-> simpletext = "host resolved";} // ------------------------- --------------------------------------------------
Void __fastcall tform1 :: nmstrMserv1clientContact (TOBJECT * Sender) {statusbar1-> simpletext = "client contact";} //----------------------- -------------------------------------------------- Void __fastcall tform1 :: nmstrMserv1status (tcomponent * sender, Ansistring status) {if (statusbar1! = 0) statusbar1-> simpletext = status;} // ---------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------