 |
|
.NET ช่วยอธิบายการเขียนโปรแกรมรับส่งไฟล์ผ่านเครือข่าย ด้วยภาษา C# ผ่าน Socket หน่อยค่ะ |
|
 |
|
|
 |
 |
|
พี่ท่านใดรู้เรื่องนี้ช่วยตอบให้ทีสิค่ะ
|
 |
 |
 |
 |
Date :
2011-03-16 16:20:40 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เคยเขียนส่งผ่าน Socket น่ะครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Reflection;
class MainClass
{
public static void Main()
{
IPAddress host = IPAddress.Parse("192.168.104.104");
IPEndPoint hostep = new IPEndPoint(host, 8080);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sock.Connect(hostep);
} catch (SocketException e) {
Console.WriteLine("Not Connect to host");
Console.WriteLine(e.ToString());
sock.Close();
return;
}
try
{
sock.Send(Encoding.ASCII.GetBytes("testing"));
} catch (SocketException e) {
Console.WriteLine("Problem sending data");
Console.WriteLine( e.ToString());
sock.Close();
return;
}
sock.Close();
}
}
Go to : .NET เขียน Transfer file ผ่าน Socket ระหว่าง PC กับ Handheld
|
 |
 |
 |
 |
Date :
2011-03-16 20:42:44 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพี่ mr.win ที่ช่วยตอบค่ะ
แต่ทำมัยของหนูรันแล้วติดที่บรรทัด
using System.Drawing;
และก็
using System.Windows.Forms;
มันเป็นที่ปัญหาอะรัยค่ะ
|
 |
 |
 |
 |
Date :
2011-03-16 21:46:28 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ค่ะ แต่ถ้าไม่ประกาศ
using System.Drawing;
และก็
using System.Windows.Forms;
มันจะติด error ที่ตัว Console. ทั้งสี่ตัวเลยนะค่ะ
คือหนูไม่ค่อยมีความรู้เรื่องนี้เลยค่ะ
พึ่งเคยเรียน แต่อาจาร์ให้ทำส่งเลยงงมากค่ะ
|
 |
 |
 |
 |
Date :
2011-03-16 22:01:10 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ค่ะโค้ดตัวนี้เวลารันได้แล้ว
มันจะเป็นยังงัยค่ะ พอจะมีตัวอย่างให้ดูมั้ยค่ะ
ขอบคุณล่วงหน้าค่ะ
|
 |
 |
 |
 |
Date :
2011-03-16 22:14:20 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ค่ะ โค้ดตัวนี้เวลารันแล้วมันจะขึ้น Not Connect to host
ซึ้งหนูเข้าใจว่ามันไม่สามารถติดต่อกับ server ได้ใช่มั้ยค่ะ
แต่ก็ทำตัว server แล้วนะค่ะ แต่ก็ยังไม่ได้เหมือนเดิม
พี่พอจะมีคำแนะนำเพิ่มเติมมั้ยค่ะ
หรือพอมีตัวอย่างโค้ดที่ server กับ client สามารถส่งไฟล์หากันได้มั้ยค่ะ
ถ้าได้แบบละเอียดจะเป็นพระคุณมากเลยค่ะ
ขอบคุณล่วงหน้าค่ะ
|
 |
 |
 |
 |
Date :
2011-03-17 18:10:58 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพี่ mr.win ที่ช่วยตอบข้อสงสัยค่ะ
ที่ไปศึกษาและทำตามเรื่องการติดต่อ Socket นะค่ะ
ทำมัยเวลารันแล้วพิมอะรัยมันก็หายไปค่ะ
เราต้องทำยังให้มันรับข้อมูลถึงกันได้ค่ะ
|
 |
 |
 |
 |
Date :
2011-03-17 21:52:06 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมมีหนังสือที่สอนการเขียน tranfer file ผ่านเครือข่ายอยู่
บทที่ 5 tcp
บทที่ 6 udp
สนใจ post mail ไว้ครับ

|
 |
 |
 |
 |
Date :
2011-03-18 11:48:55 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพี่ pheak ที่ช่วยค่ะ
เมล์ [email protected] ค่ะ
|
 |
 |
 |
 |
Date :
2011-03-18 14:23:35 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อยากทราบว่าจะแปลงไฟล์ประเภทต่างๆ ให้เป็น bit ได้ยังงัยค่ะ
คืออยากรู้ว่าคิดถูกมั้ยค่ะว่าเราต้องแปลงไฟล์ให้เป็น bit ให้ได้ก่อน
แล้วเราถึงจะสามารถส่งไฟล์ได้
อยากให้พี่ๆ ผู้รู้ช่วยอธิบายให้หน่อยค่ะ
|
 |
 |
 |
 |
Date :
2011-03-21 14:51:36 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมจะต้องแปลงเป็น bit ครับ 
|
 |
 |
 |
 |
Date :
2011-03-21 16:25:18 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือยังไม่เข้าใจว่าเราจะเขียนให้โปรแกรมส่งไฟล์ เช่น ไฟล์ภาพ ไฟล์เพลง พวกไฟล์ประเภทoffice หรือเป็นไฟล์ประเภทอื่นได้ยังงัยค่ะ
ขออภัยในความไม่รู้
อยากให้ผู้รู้ช่วยตอบด้วยค่ะ
|
 |
 |
 |
 |
Date :
2011-03-22 12:40:42 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองหาดูครับใน google
search คำว่า C# tcp tranfer file หรือ C# udp tranfer file
มีคนเขาเขียนไว้เต็มครับ สำคัญว่าจะนำไปประยุกต์ใช้ได้ไหม สำคัญเลยคือพื้นฐานด้าน C#
http://www.codeproject.com/KB/cs/SocketApplication.aspx
http://www.csharphelp.com/2006/10/client-server-simple-file-sharing-and-file-transfer/
|
 |
 |
 |
 |
Date :
2011-03-22 16:34:42 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษนะคับ
ผมอ่านดูมีแต่ภาษาอังกษฤไม่รู้เรื่องคัยช่วยอธิบายหัยเข้าใจหน่อยคับ
พอดีไม่ค่อยเข้าใจเรื่องของโค้ดคับ ว่าโค้ดแต่ละบรรทัดเป็นยังไง
คัยพออธิบายได้ขอบคุณมากคับ
http://www.codeproject.com/KB/cs/SocketApplication.aspx
http://www.csharphelp.com/2006/10/client-server-simple-file-sharing-and-file-transfer/
สำหรับเว็บไปอ่านแล้วก็ดีนะคับแต่ไม่ค่อยเข้าใจไม่เคือบหน้าเลยอธิบายหัยหน่อยคับ
คัยพออธิบายได้ขอบคุณมากคับ
|
 |
 |
 |
 |
Date :
2011-03-23 12:54:14 |
By :
kit |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เข้าใจค่ะที่อ่านภาษาอังกริดไม่รู้เรื่อง
เพราะอ่านภาษาของคุณไม่ค่อยจะรุเรื่องเลย
|
 |
 |
 |
 |
Date :
2011-03-23 15:47:11 |
By :
สาวเอ๋อ เอ๋อเหรอ เอ๋อมากมาย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ pheak ครับ ผมสนใจหนังสือ ของคุณน่ะครับ
รบกวนส่งให้ทางเมลล์ ได้รึเปล่าครับ
email : [email protected]
|
 |
 |
 |
 |
Date :
2011-03-23 17:50:01 |
By :
vanz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอดี พี่ pheak ส่งหนังสือมาให้แล้วค่ะ
เดี๋ยวจะส่งต่อให้นะค่ะ แบ่งปันความรู้
ขอบคุณ พี่ pheak ด้วยค่ะที่ให้หนังสือมา
|
 |
 |
 |
 |
Date :
2011-03-24 14:09:27 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียนได้ยังไง รบกวนเขียนเป็นตัวอย่างซะ 1 ตัวน่ะครับ เผื่อจะเป็นประโยนช์ต่อสมาชิกคนอื่น ๆ ด้วยครับ
|
 |
 |
 |
 |
Date :
2011-03-24 17:32:12 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเขียนได้แล้วจะให้ดูตัวอย่างนะค่ะ
แต่ตอนนี้ยังทำไม่ได้เลยค่ะ...................
ใครมีวิธีก็ช่วยแนะนำด้วยนะค่ะ
|
 |
 |
 |
 |
Date :
2011-03-28 00:44:16 |
By :
aofaff |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอหนังสือด้วยได้ไหมครับ
ถ้าได้จะขอบคุณมากเลยกำลังทำโปรเจคเกี่ยวกับพวกนี้อยู่
มึนๆ งงๆ ได้ข้อมูลจากเว็ปนี้ไปมากมายเลย ขอบคุณเว็ปดีๆนี้ด้วยแล้วกันครับ
E-mail ผมนะ [email protected]
|
 |
 |
 |
 |
Date :
2011-04-04 16:38:43 |
By :
Rikisawa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมขอหนังสือด้วยนะครับ
ถ้าได้แล้วเดี๋ยวจะเอามาแชร์ เพื่อเป็นประโยชน์แก่คนอื่นๆครับ
mail :: [email protected]
|
 |
 |
 |
 |
Date :
2011-05-20 21:01:08 |
By :
วิเชียร โตโส |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลอง PM ไปน่ะครับ 
|
 |
 |
 |
 |
Date :
2011-05-21 09:21:46 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอหนังสือด้วยอีกคนนะคะ
[email protected]
ขอบคุณล่วงหน้านะคะ
|
 |
 |
 |
 |
Date :
2011-05-22 23:17:07 |
By :
เอย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมขอหนังสือด้วยนะคับ
[email protected]
ขอบคุณมากๆเลยคับ
|
 |
 |
 |
 |
Date :
2011-07-14 16:20:10 |
By :
Rookie |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
//FILE TRANSFER USING C#.NET SOCKET - SERVER
class FTServerCode
{
IPEndPoint ipEnd;
Socket sock;
public FTServerCode()
{
ipEnd = new IPEndPoint(IPAddress.Any, 5656);
//Make IP end point to accept any IP address with port no 5656.
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
//Here creating new socket object with protocol type and transfer data type
sock.Bind(ipEnd);
//Bind end point with newly created socket.
}
public static string receivedPath;
public static string curMsg = "Stopped";
public void StartServer()
{
try
{
curMsg = "Starting...";
sock.Listen(100);
/* That socket object can handle maximum 100 client connection at a time &
waiting for new client connection /
curMsg = "Running and waiting to receive file.";
Socket clientSock = sock.Accept();
/* When request comes from client that accept it and return
new socket object for handle that client. */
byte[] clientData = new byte[1024 * 5000];
int receivedBytesLen = clientSock.Receive(clientData);
curMsg = "Receiving data...";
int fileNameLen = BitConverter.ToInt32(clientData, 0);
/* I've sent byte array data from client in that format like
[file name length in byte][file name] [file data], so need to know
first how long the file name is. /
string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
/* Read file name */
BinaryWriter bWrite = new BinaryWriter(File.Open
(receivedPath +"/"+ fileName, FileMode.Append)); ;
/* Make a Binary stream writer to saving the receiving data from client. /
bWrite.Write(clientData, 4 + fileNameLen,
receivedBytesLen - 4 - fileNameLen);
/* Read remain data (which is file content) and
save it by using binary writer. */
curMsg = "Saving file...";
bWrite.Close();
clientSock.Close();
/* Close binary writer and client socket */
curMsg = "Received & Saved file; Server Stopped.";
}
catch (Exception ex)
{
curMsg = "File Receiving error.";
}
}
}
Code (C#)
//FILE TRANSFER USING C#.NET SOCKET - CLIENT
class FTClientCode
{
public static string curMsg = "Idle";
public static void SendFile(string fileName)
{
try
{
IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
/* Make IP end point same as Server. */
Socket clientSock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.IP);
/* Make a client socket to send data to server. */
string filePath = "";
/* File reading operation. */
fileName = fileName.Replace("\\", "/");
while (fileName.IndexOf("/") > -1)
{
filePath += fileName.Substring(0, fileName.IndexOf("/") + 1);
fileName = fileName.Substring(fileName.IndexOf("/") + 1);
}
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
if (fileNameByte.Length > 850 * 1024)
{
curMsg = "File size is more than 850kb, please try with small file.";
return;
}
curMsg = "Buffering ...";
byte[] fileData = File.ReadAllBytes(filePath + fileName);
/* Read & store file byte data in byte array. */
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
/* clientData will store complete bytes which will store file name length,
file name & file data. */
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
/* File name length’s binary data. */
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
/* copy these bytes to a variable with format line [file name length]
[file name] [ file content] */
curMsg = "Connection to server ...";
clientSock.Connect(ipEnd);
/* Trying to connection with server. /
curMsg = "File sending...";
clientSock.Send(clientData);
/* Now connection established, send client data to server. */
curMsg = "Disconnecting...";
clientSock.Close();
/* Data send complete now close socket. */
curMsg = "File transferred.";
}
catch (Exception ex)
{
if(ex.Message=="No connection could be made because the target machine
actively refused it")
curMsg="File Sending fail. Because server not running." ;
else
curMsg = "File Sending fail." + ex.Message;
}
}
}
http://www.codeproject.com/KB/cs/SocketApplication.aspx
|
 |
 |
 |
 |
Date :
2011-07-22 17:56:17 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอผู้ใจบุญช่วยเขียนโปรแกม c#2008 ให้หน่อยครับคือว่าผมเขียนเท่าไรก็ไม่ได้ซักทีพอเขียนแล้วก็ได้ไม่ตรงช่วย
หน่อยนะครับขอบพระคุณมากครับ
1.รับข้อมูลจำนวนเต็มจากผุ้ใช้ จะหยุดรับข้อมูลเมื่อผลรวมของข้อมูลนั้น มีค่ามากกว่า 200 ให้หาว่ามี่การรับข้อมูล ทั้งหมดกี่จำนวน
2.เขียนโปรแกรมเพื่อรับข้อมูลตัวอักษารจากผู้ใช้ หากผู้ใช้ป้อนตัวอักษร a,b.x ให้ขึ้นข้อมความว่า "Hanaga"ป้อนตัวอักษร u,d,p ให้ขึ้นข้อความว่า "Bingo" ป้อนตัวอักษร g ให้ขึ้นข้อความว่า "Google" ป้อนตัวอักษรอื่น ๆ ให้ขึ้นข้อความว่า "Yappadappadoooo"
3.เขียนโปรแกรมเพื่อบวกเลขจำนวนเต็มคี่ N จำนวนโดยรับค่า N จากผู้ใช้
|
 |
 |
 |
 |
Date :
2011-08-05 16:02:43 |
By :
maxky |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอหนังสือด้วยคนค่ะ
[email protected]
รบกวนด้วยนะค่ะ ขอบคุณค่ะ
|
 |
 |
 |
 |
Date :
2014-02-11 04:35:32 |
By :
1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนขอไฟล์หนังสือด้วยคนครับ คุณ pheak
[email protected]
|
 |
 |
 |
 |
Date :
2014-07-17 09:07:22 |
By :
Wee11 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนขอไฟล์หนังสือด้วยคนครับ
[email protected]
ขอบคุณล่วงหน้าครับ
|
 |
 |
 |
 |
Date :
2014-09-21 22:08:51 |
By :
จักรินทร์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กำลังจะทำโปรเจ็คจบโปรแกรมร้านเน็ตพอดี ขอด้วยคนครับผม 
[email protected][b][/b]
|
 |
 |
 |
 |
Date :
2014-09-27 04:03:31 |
By :
ฺBenjamin Intarabarn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอหนังสือด้วยคนค่ะ
ขอบคุณล่วงหน้าค่ะ
[email protected]
|
 |
 |
 |
 |
Date :
2018-08-19 16:41:08 |
By :
คุกคิก |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|