Untitled
unknown
plain_text
4 years ago
9.2 kB
5
Indexable
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Sockets;
using System.Threading;
using System.IO;
}
namespace socket_programming
{
public partial class Form1 : Form
{
bool terminating = false;
bool connected = false;
Socket clientSocket;
public Form1()
{
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
private void button_connect_click(object sender, EventArgs e)
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
string IP = textBox1_ip.Text;
int portNum;
if (Int32.TryParse(textBox2_port.Text, out portNum))
{
try
{
clientSocket.Connect(IP, portNum);
button2_createAccount.Enable = true; //initially false
textBox3_name.Enabled = true; //initially false
textBox4_surname.Enabled = true;
textBox5_username.Enabled = true;
textBox6_password.Enabled = true;
button_connect.Enabled = false;
connected = true;
clientlogs.AppendText("You are connected!\n");
Thread receivethread = new Thread(Receive);
recievethread.Start();
}
catch
{
//if the server is not listed
clientlogs.AppendText("Could not connect to the server!\n");
}
}
else
{
clientlogs.AppendText("Check your port number!\n");
}
}
private void Receive()
{
while (connected)
{
try
{
Byte[] username = new byte[64]; //message travels as a byte array form such as usernames serder'dan yazdir
clientSocket.Receive(username);
string received_username = Encoding.Default.GetString(username);
received_username = received_username.Substring(0, received_username.IndexOf("\0"));
//suggestion: you can use .Trim('\0')
}
catch
{
if (!terminating)
{
clientlogs.AppendText("The server has disconnected.\n");
button_connect.Enabled = true;
textBox3_name.Enabled = false;
textBox4_surname.Enabled = false;
textBox5_username.Enabled = false;
textBox6_password.Enabled = false;
button2_createAccount.Enabled = false;
}
clientSocket.Close();
connected = false;
}
var lines = File.ReadLines(@"/Users/bengisuozdemir/Documents/database.txt");
using (StreamWriter file = new StreamWriter("/Users/bengisuozdemir/Documents/database.txt", append: true))
{
file.WriteLine(username + "-");
}
}
}
private void button2_createAccountClick(object sender, EventArgs e)
{
string Name = textBox3_name.Text();
string Surname = textBox4_surname.Text();
string Username = textBox5_username.Text();
string Password = textBox6_password.Text();
if (Username != "")//also can check the lenght if it's equal to 64 bytes
{
Byte[] bufferU = Encoding.Default.GetBytes(Username);
clientSocket.Send(bufferU);
if(true)//if the .txt file does not contain entered username
{
clientlogs.AppendText("You have created a new account!\n");
}
else
{
clientlogs.AppendText("There is already an account with this username!\n");
}
}
else
{
clientlogs.AppendText("Please enter a username!\n");
}
if (Name != "")//also can check the lenght if it's equal to 64 bytes
{
Byte[] bufferN = Encoding.Default.GetBytes(Name);
clientSocket.Send(bufferN);
}
else
{
clientlogs.AppendText("Please enter a name!\n");
}
if (Surname == "")//also can check the lenght if it's equal to 64 bytes
{
Byte[] bufferS = Encoding.Default.GetBytes(Surname);
clientSocket.Send(bufferS);
}
else
{
clientlogs.AppendText("Please enter a surname!\n");
}
if (Password != "")//also can check the lenght if it's equal to 64 bytes
{
Byte[] bufferP = Encoding.Default.GetBytes(Password);
clientSocket.Send(bufferP);
}
else
{
clientlogs.AppendText("Please enter a password!\n");
}
}
private void Form1_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
connected = false;
terminating = true;
Environment.Exit(0);
}
}
}
////
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Threading;
namespace server
{
public partial class Form1 : Form1
{
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
List<Socket> clientSockets = new List<Socket>();
bool terminating = false;
bool listening = false;
public Form1()
{
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
private void button_listen_Click(object sender, EventArgs e)
{
int serverPort;
if (Int32.TryParse(textBox_port.Text, out serverPort))
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, serverPort);
serverSocket.Bind(endPoint);
serverSocket.Listen(3); //get username status
listening = true;
button_listen.Enabled = false;
textBox_message.Enabled = true;
button_createAcccount = true;
Thread acceptThread = new Thread(Accept);
acceptThread.Start();
logs.AppendText("");
}
else
{
logs.AppendText("");
}
}
private void Accept()
{
while (listening)
{
try
{
Socket newClient = serverSocket.Accept();
clientSockets.Add(newClient);
logs.AppendText("A client has connected!\n");
Thread recieveThread = new Thread(() => Receive(newClient));
}
catch
{
if (terminating)
{
listening = false;
}
else
{
logs.AppendText("The socket stopped working.\n");
}
}
}
}
private void Receive(Socket thisClient)
{
bool connected = true;
while(connected && !terminating)
{
try
{
Byte[] buffer = new byte[64];
thisClient.Receive(buffer);
string incomingUsername = Encoding.Default.GetString(buffer);
incomingUsername = incomingUsername.Substring(0, incomingUsername.IndexOf("\0"));
logs.AppendText(incomingUsername + " has created an account.");
}
catch
{
if (!terminating)
{
logs.AppendText("A client has disconnected.\n");
}
thisClient.Close();
clientSockets.Remove(thisClient);
connected = false;
}
}
}
}
}
Editor is loading...