示一下BCB 不用控件的Socket通信例子
大家好:
本人想学一下Socket通信方面的知识,在这时求一个BCB(BCB2007)不控件的Socket通信的例子。
先谢谢大家!!
[解决办法]
傳送螢幕
====================
Client端
#include
byte cSendTemp[1024];
ms1 = new TMemoryStream;
//使用jpg
jpg = new TJPEGImage;
jpg->LoadFromFile("01.jpg");
jpg->SaveToStream(ms1);
delete jpg;
// 呼叫 WSAStartup() 註冊 WinSock DLL 的使用
if ( WSAStartup( 0x101,&wsadata) != 0)
{
ShowMessage("winsock_server: can't use WinSock DLL\n");
exit(1);
}
// 開啟 UDP socket
if ( (cli_sd=socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR)
{
ShowMessage("Winsock_server: can't open TCP socket\n");
exit(1);
}
//指定 socket 的 IP 位址和 port number
cli.sin_family = AF_INET;
cli.sin_addr.s_addr = inet_addr(Edit1->Text.c_str());
cli.sin_port = htons(6010);
cli_len = sizeof(cli);
int iTotal=ms1->Size;
int iPacket=sizeof(cSendTemp);
int iTime=(iTotal%iPacket==0)? iTotal/iPacket: iTotal/iPacket+1;
//ShowMessage((AnsiString)iTime);
for(int i=0; i
{
ms1->Position=i*iPacket;
int iLen1=(i
ZeroMemory(&cSendTemp, sizeof(cSendTemp));
ms1->Read(cSendTemp, iLen1);
Sleep(10);
if ( sendto( cli_sd, cSendTemp, sizeof(cSendTemp) + 1, 0, (LPSOCKADDR)&cli, cli_len) == SOCKET_ERROR)
ShowMessage("SendToTest: send() error!\n");
Sleep(10);
}
if ( sendto( cli_sd, 0, 0, 0, (LPSOCKADDR)&cli, cli_len) == SOCKET_ERROR)
ShowMessage("SendToTest: send() error!\n");
closesocket(cli_sd);
WSACleanup();
Server端
#include
byte cServTemp[1024];
// 呼叫 WSAStartup() 註冊 WinSock DLL 的使用
if ( WSAStartup( 0x101, (LPWSADATA)&wsadata) != 0 )
{
ShowMessage("FileServer: can't use WinSock DLL\n");
exit(1);
}
// 開啟 UDP socket
if ( ( serv_sd = socket( AF_INET, SOCK_DGRAM, 0) ) == SOCKET_ERROR)
{
ShowMessage("FileServer: can't open TCP socket\n");
exit(1);
}
// 指定 socket 的 IP 位址和 port number
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = 0;
serv.sin_port = htons(6010);
//將本機的資訊寫入serv結構中
if (( bind( serv_sd, (LPSOCKADDR)&serv, sizeof(serv))) == SOCKET_ERROR )
{
ShowMessage("FileServer: can't bind local address\n");
exit(1);
}
int count = 0;
jpg = new TJPEGImage;
serv_len = sizeof(serv);
Memo1->Lines->Add("Wait for clinet.....");
Memo1->Refresh();
//進入接收的while loop
while(1)
{
ZeroMemory(&cServTemp, sizeof(cServTemp));
n = recvfrom( serv_sd, cServTemp, 1024, 0, (LPSOCKADDR)&serv, &serv_len);
if ((n != 0)&&(n != SOCKET_ERROR))
{
ms1->Position = count*1024;
ms1->Write(cServTemp,sizeof(cServTemp));
count++;
//Memo1->Lines->Add((AnsiString)count);
}
if (n == 0)
{
Memo1->Lines->Add("Transmite completed!");
Memo1->Realign();
closesocket(serv_sd);
break;
}
else if ( n == SOCKET_ERROR)
{
ShowMessage("FileServer: recv() error!");
closesocket(serv_sd);
break;
}
Memo1->Refresh();
}
ms1->Position = 0;
jpg->LoadFromStream(ms1);
jpg->SaveToFile("01.jpg");
WSACleanup();
[解决办法]
看看linux下的socket就可以了。
------解决方案--------------------
http://download.csdn.net/source/727569
[解决办法]
在CB中用socket api来写网络通讯程序
http://www.ccrun.com/article.asp?i=569&d=1w5p2y
[解决办法]
简单点的就是获取本机IP地址,用socket api
void __fastcall TForm1::FormCreate(TObject *Sender)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested=MAKEWORD(2,0);
err=WSAStartup( wVersionRequested,&wsaData );
if( err !=0 )
{
MessageBox(NULL,"Can't initialize WinSock!","Error",MB_OK);
return;
}
if(LOBYTE( wsaData.wVersion )!=2||HIBYTE( wsaData.wVersion )!=0 )
{
WSACleanup( );
MessageBox(NULL,"Can't initialize WinSock!","Error",MB_OK);
return;
}
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char HostName[80];
struct in_addr in;
LPHOSTENT lpHostEnt;
char *ptr;
memset((void *)&in,sizeof(in),0);
if(gethostname(HostName,sizeof(HostName))==SOCKET_ERROR)
{
MessageBox(NULL,"Can't getting localhost name.","Error",MB_OK);
return;
}
Edit1->Text=HostName;
lpHostEnt=gethostbyname(HostName);
if(!lpHostEnt)
{
MessageBox(NULL,"bad host lookup.","Error",MB_OK);
return;
}
in.s_addr=*((unsigned long*)lpHostEnt->h_addr_list[0]);
if(!(ptr=inet_ntoa(in)))
return;
Edit2->Text=inet_ntoa(in);
WSACleanup();
}
[解决办法]