0% found this document useful (0 votes)
49 views

Error in Code

This document contains code for a server that accepts TCP client connections. It defines methods for accepting new clients, reading data from clients, and copying files received from clients. When a new client connects, it is added to lists and has its endpoint displayed. A thread is started to read data from the client. The read method checks for available data, splits received strings, and writes received bytes to files with the name and length specified in the bytes.

Uploaded by

arshadyab
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Error in Code

This document contains code for a server that accepts TCP client connections. It defines methods for accepting new clients, reading data from clients, and copying files received from clients. When a new client connects, it is added to lists and has its endpoint displayed. A thread is started to read data from the client. The read method checks for available data, splits received strings, and writes received bytes to files with the name and length specified in the bytes.

Uploaded by

arshadyab
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

public void AcceptClient(IAsyncResult result) { try { object obj = result.AsyncState; TcpListener list = obj as TcpListener; client = list.EndAcceptTcpClient(result); clobj.

Add(client); selectedClient.Add(client); MessageBox.Show(client.Client.RemoteEndPoint.ToString());

list

list //

Thread th = new Thread(ReadData); th.Start(client); // Thread th1 = new Thread(CopyFile); ager main isay user karta hon Read thread k sath to sirf file recv karta hay phr read data wala thread nahe chalata // th1.Start(client); list.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), list); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public void ReadData(object obj) { try { client = obj as TcpClient; NetworkStream ns = client.GetStream(); while (true) { if (ns.DataAvailable) {

StreamReader sr = new StreamReader(ns); string tem = ""; string[] login; tem = sr.ReadLine(); login = tem.Split('|'); if (login[0] == "ID") { MsgToTree(tem,client.Client.RemoteEndPoint.ToString());

MessageBox.Show(tem); ns.Flush(); } Yahan say data byte form main a rah ahay jis ki waja say sanme ki fileNamelen =0 k baraber ho ja rahe hay pahlay jo stream reader nay read ker li values else { string receivedPath = GetReceivedPath(); List<byte> mybyte = new List<byte>(); while (ns.DataAvailable) { mybyte.Add(Convert.ToByte(ns.ReadByte())); } //int receivedBytesLen = cns.Read(clientData, 0, clientData.Length);// clientSock.Receive(clientData);

int fileNameLen = BitConverter.ToInt32(mybyte.ToArray(), 0); string fileName = Encoding.ASCII.GetString(mybyte.ToArray(), 4, fileNameLen); // Encoding.ASCII.GetString BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); bWrite.Write(mybyte.ToArray(), 4 + fileNameLen, mybyte.Count - 4 - fileNameLen);

MessageBox.Show("File Has Been Copied"); bWrite.Close(); } // ns.Flush(); // MsgToTree(tem); } Thread.Sleep(1000); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private static string GetReceivedPath() { string RecvFolder = "RecvData";//ConfigurationManager.AppSettings["Path"]; string receivedPath = @"C:\" + RecvFolder + "\\"; return receivedPath; }

public void CopyFile(object nm) { try { TcpClient kl = nm as TcpClient; //ns = kl.GetStream(); StreamReader sr = new StreamReader(ns); //string[] check = sr.ReadLine().Split('|'); //if (check[0] == "ID") //{ // Thread th = new Thread(ReadData); // th.Start(client); //} //string RecvFolder = "RecvData";//ConfigurationManager.AppSettings["Path"]; //string receivedPath = @"C:\" + RecvFolder + "\\";//ConfigurationManager.AppSettings["Path"]; NetworkStream cns; cns= kl.GetStream(); // byte[] clientData = new byte[1024 * 5000]; while (true) { if (cns.DataAvailable) {

string receivedPath = GetReceivedPath(); List<byte> mybyte = new List<byte>(); while (cns.DataAvailable) { mybyte.Add(Convert.ToByte(cns.ReadByte())); } //int receivedBytesLen = cns.Read(clientData, 0, clientData.Length);// clientSock.Receive(clientData);

int fileNameLen = BitConverter.ToInt32(mybyte.ToArray(), 0); string fileName = Encoding.ASCII.GetString(mybyte.ToArray(), 4, fileNameLen); // Encoding.ASCII.GetString BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); bWrite.Write(mybyte.ToArray(), 4 + fileNameLen, mybyte.Count - 4 - fileNameLen);

MessageBox.Show("File Has Been Copied"); bWrite.Close(); // } // cns.Flush(); } cns.Flush(); Thread.Sleep(1000); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }

You might also like