PDA

View Full Version : مشکل در سوکت پروگرامینگ فوری فوری فوری خواهشا



hooty70
یک شنبه 11 تیر 1391, 20:44 عصر
با سلام من پروژه پایان ترمم چت متنی کلاینت سرور با قابلیت ارسال فایل هست
یه مشکلی هست من کد رو نوشتم ولی نمی دونم چه کدی رو کجا بنویسم که سرور بتونه پاسخ بده و چت دو طرفه باشه برای ارسال فایلم چیکار کنم لطفا راهنماییم کنید در ضمن تحت کنسول نوشتم
سمت سرور


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Server
{
class Program
{
static byte[] Buffer { get; set; }
static Socket sck;
static void Main(string[] args)
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint IPEP = new IPEndPoint(0, 1234);
sck.Bind(IPEP);
Console.WriteLine("-------------------- Network & Connection Information Summery ------------------");
Console.WriteLine("Application Type : Client - Server / Server - Client\n");
Console.WriteLine("Your System Type : Server\n");
Console.WriteLine("Network IP End Point : {0}\n", IPEP.ToString());
Console.WriteLine("Network Address Family : {0}\n", IPEP.AddressFamily);
Console.WriteLine("Network address : {0} - Port : {1}\n", IPEP.Address, IPEP.Port);
Console.WriteLine("Network Protocol Type : {0}\n", sck.ProtocolType);
Console.WriteLine("-------------------- Network & Connection Information Summery ------------------");
sck.Listen(100);
Socket accepted = sck.Accept();
while (true)
{
Buffer = new byte[accepted.SendBufferSize];
int bytesRead = accepted.Receive(Buffer);
byte[] formatted = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
{
formatted[i] = Buffer[i];
}
string strData = Encoding.ASCII.GetString(formatted);
if (strData == "exit")
break;
Console.Write("Client :" + strData + "\r\n");
}
sck.Close();
accepted.Close();
}
}
}



سمت کلاینت

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Client
{
class Program
{
static Socket sck;
static void Main(string[] args)
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
Console.WriteLine("-------------------- Network & Connection Information Summery ------------------");
Console.WriteLine("Application Type : Client - Server / Server - Client\n");
Console.WriteLine("Your System Type : Server\n");
Console.WriteLine("Network IP End Point : {0}\n", localEndPoint.ToString());
Console.WriteLine("Network Address Family : {0}\n", localEndPoint.AddressFamily);
Console.WriteLine("Network address : {0} - Port : {1}\n", localEndPoint.Address, localEndPoint.Port);
Console.WriteLine("Network Protocol Type : {0}\n",sck.ProtocolType);
Console.WriteLine("-------------------- Network & Connection Information Summery ------------------");
//-----------------------------------------Main Codes----------------------------------------------------------------
try
{
sck.Connect(localEndPoint);
}
catch
{
Console.Write("Unable to connect to Server...!\r\n");
Main(args);
}
while (true)
{
Console.Write("Enter Text: ");
string text = Console.ReadLine();
if (text == "exit")
break;
byte[] data = Encoding.ASCII.GetBytes(text);
sck.Send(data);

}
sck.Close();
}
}
}

hunter_ara
دوشنبه 12 تیر 1391, 11:26 صبح
با سلام من پروژه پایان ترمم چت متنی کلاینت سرور با قابلیت ارسال فایل هست
یه مشکلی هست من کد رو نوشتم ولی نمی دونم چه کدی رو کجا بنویسم که سرور بتونه پاسخ بده و چت دو طرفه باشه برای ارسال فایلم چیکار کنم لطفا راهنماییم کنید در ضمن تحت کنسول نوشتم
سمت سرور


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Server
{
class Program
{
static byte[] Buffer { get; set; }
static Socket sck;
static void Main(string[] args)
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint IPEP = new IPEndPoint(0, 1234);
sck.Bind(IPEP);
Console.WriteLine("-------------------- Network & Connection Information Summery ------------------");
Console.WriteLine("Application Type : Client - Server / Server - Client\n");
Console.WriteLine("Your System Type : Server\n");
Console.WriteLine("Network IP End Point : {0}\n", IPEP.ToString());
Console.WriteLine("Network Address Family : {0}\n", IPEP.AddressFamily);
Console.WriteLine("Network address : {0} - Port : {1}\n", IPEP.Address, IPEP.Port);
Console.WriteLine("Network Protocol Type : {0}\n", sck.ProtocolType);
Console.WriteLine("-------------------- Network & Connection Information Summery ------------------");
sck.Listen(100);
Socket accepted = sck.Accept();
while (true)
{
Buffer = new byte[accepted.SendBufferSize];
int bytesRead = accepted.Receive(Buffer);
byte[] formatted = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
{
formatted[i] = Buffer[i];
}
string strData = Encoding.ASCII.GetString(formatted);
if (strData == "exit")
break;
Console.Write("Client :" + strData + "\r\n");
}
sck.Close();
accepted.Close();
}
}
}



سمت کلاینت

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Client
{
class Program
{
static Socket sck;
static void Main(string[] args)
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
Console.WriteLine("-------------------- Network & Connection Information Summery ------------------");
Console.WriteLine("Application Type : Client - Server / Server - Client\n");
Console.WriteLine("Your System Type : Server\n");
Console.WriteLine("Network IP End Point : {0}\n", localEndPoint.ToString());
Console.WriteLine("Network Address Family : {0}\n", localEndPoint.AddressFamily);
Console.WriteLine("Network address : {0} - Port : {1}\n", localEndPoint.Address, localEndPoint.Port);
Console.WriteLine("Network Protocol Type : {0}\n",sck.ProtocolType);
Console.WriteLine("-------------------- Network & Connection Information Summery ------------------");
//-----------------------------------------Main Codes----------------------------------------------------------------
try
{
sck.Connect(localEndPoint);
}
catch
{
Console.Write("Unable to connect to Server...!\r\n");
Main(args);
}
while (true)
{
Console.Write("Enter Text: ");
string text = Console.ReadLine();
if (text == "exit")
break;
byte[] data = Encoding.ASCII.GetBytes(text);
sck.Send(data);

}
sck.Close();
}
}
}



برات 1 کتاب (http://www.mediafire.com/view/?2c278l6o6g7ralt)میزارم و چنتا توصیه
از Tcp/IP استفاده کن
و WinForm(در غیر این صورت مجبوری hardcode کنی و در نهایت برای انتخواب فایل از OpenFileDialog استفاده کنی که تو کنسول پذیرفته نیست! خلاصش این میشه که پدرت در میاد)
راستی یادم رفت چون چت دوطرفه میخای باید Multithered بنویسی! (1ای باید فقط ارسال کنه و دیگری فقط دریافت!)