1. linux下socket 網路編程(客戶端向伺服器端發送文件) 求源代碼 大哥大姐幫幫忙 ,。。謝謝
源代碼奉上,流程圖。。。這個太簡單了,你自己看看。。。。。。。
//TCP
//伺服器端程序
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define BACKLOG 10
#define TRUE 1
void main( void )
{
int iServerSock;
int iClientSock;
char *buf = "hello, world!\n";
struct sockaddr_in ServerAddr;
struct sockaddr_in ClientAddr;
int sin_size;
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
if( ( iServerSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "創建套接字失敗!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//監視的埠號
ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind調用失敗!\n" );
WSACleanup( );
exit( 0 );
}
if( listen( iServerSock, BACKLOG ) == -1 )
{
printf( "listen調用失敗!\n" );
WSACleanup( );
exit( 0 );
}
while( TRUE )
{
sin_size = sizeof( struct sockaddr_in );
iClientSock = accept( iServerSock, ( struct sockaddr * )&ClientAddr, &sin_size );
if( iClientSock == -1 )
{
printf( "accept調用失敗!\n" );
WSACleanup( );
exit( 0 );
}
printf( "伺服器連接到%s\n", inet_ntoa( ClientAddr.sin_addr ) );
if( send( iClientSock, buf, strlen( buf ), 0 ) == -1 )
{
printf( "send調用失敗!" );
closesocket( iClientSock );
WSACleanup( );
exit( 0 );
}
}
}
/////客戶端程序
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define BACKLOG 10
#define TRUE 1
#define MAXDATASIZE 100
void main( void )
{
int iClientSock;
char buf[ MAXDATASIZE ];
struct sockaddr_in ServerAddr;
int numbytes;
// struct hostent *he;
WSADATA WSAData;
// int sin_size;
/* if( ( he = gethostbyname( "liuys" ) ) == NULL )
{
printf( "gethostbyname調用失敗!" );
WSACleanup( );
exit( 0 );
}
*/
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
if( ( iClientSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "創建套接字失敗!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
// ServerAddr.sin_addr = *( ( struct in_addr * )he->h_addr );
ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );//記得換IP
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( connect( iClientSock, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "connect失敗!" );
WSACleanup( );
exit( 0 );
}
numbytes = recv( iClientSock, buf, MAXDATASIZE, 0 );
if( numbytes == -1 )
{
printf( "recv失敗!" );
WSACleanup( );
exit( 0 );
}
buf[ numbytes ] = '\0';
printf( "Received: %s", buf );
closesocket( iClientSock );
WSACleanup( );
}
/////UDP
//伺服器
#include< stdio.h >
#include< string.h >
#include< winsock.h >
#include< windows.h >
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define BACKLOG 10
#define TRUE 1
#define MAXDATASIZE 1000
void main( void )
{
int iServerSock;
// int iClientSock;
int addr_len;
int numbytes;
char buf[ MAXDATASIZE ];
struct sockaddr_in ServerAddr;
struct sockaddr_in ClientAddr;
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
iServerSock = socket( AF_INET, SOCK_DGRAM, 0 );
if( iServerSock == INVALID_SOCKET )
{
printf( "創建套接字失敗!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//監視的埠號
ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind調用失敗!\n" );
WSACleanup( );
exit( 0 );
}
addr_len = sizeof( struct sockaddr );
numbytes = recvfrom( iServerSock, buf, MAXDATASIZE, 0, ( struct sockaddr * ) & ClientAddr, &addr_len );
if( numbytes == -1 )
{
printf( "recvfrom調用失敗!\n" );
WSACleanup( );
exit( 0 );
}
printf( "got packet from %s\n", inet_ntoa( ClientAddr.sin_addr ) );
printf( "packet is %d bytes long\n", numbytes );
buf[ numbytes ] = '\0';
printf( "packet contains \"%s\"\n", buf );
closesocket( iServerSock );
WSACleanup( );
}
//客戶端
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define MAXDATASIZE 100
void main( void )
{
int iClientSock;
struct sockaddr_in ServerAddr;
int numbytes;
char buf[ MAXDATASIZE ] = { 0 };
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
if( ( iClientSock = socket( AF_INET, SOCK_DGRAM, 0 ) ) == -1 )
{
printf( "創建套接字失敗!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );//記得換IP
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
numbytes = sendto( iClientSock, buf, strlen( buf ), 0, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) );
if( numbytes == -1 )
{
printf( "sendto調用失敗!\n" );
WSACleanup( );
exit( 0 );
}
printf( "sent %d bytes to %s\n", numbytes, inet_ntoa( ServerAddr.sin_addr ) );
closesocket( iClientSock );
WSACleanup( );
}
2. 400分求vb.net,求一個socket的編程實例,詳細見問題補充:
至少需要
ImportsSystem
ImportsSystem.Net
ImportsSystem.Net.Sockets
ImportsSystem.Threading
ImportsSystem.Text
還要了解非同步與委託
簡單點的話
subtcplisten()'監聽過程
ConstLPortAsInteger=6850'本地監聽埠
DimIPaddAsIPAddress=Dns.GetHostEntry(Dns.GetHostName()).AddressList(1)』本地IP
'addresslist裡麵包括IPV6和IPV4
DimIPEPAsNewIPEndPoint(IPadd,LPort)'結點
TCPL=NewTcpListener(IPEP)』建立監聽實例
TCPL.Start()'開始監聽
IfTCPL.Pending=TrueThen'如果有連接接入
DimTCPLXAsNewSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)'建立一個新的SOCKET
TCPLX=TCPL.Accept()』將第一個接入掛起的連接傳給新建SOCKET,因為監聽的SOCKET還要繼續監聽
dimRipepasnewipendpoint=TCPLX.RemoteEndPoint'此時獲得遠程的ip與埠號
listbox.add(ripep.ip)
dimbuffasbyte()
buff=TCPLX.Receive()'listen>accept>recieve這時候可以接受數據了
dimsasstring=Encoding.Default.GetString(buff)'轉換成文本
msg"s"
TCPLX.send(buff)'這個是發送
endif
EndSub
長時間沒寫,可能有點小錯誤,你自己調試調試,大概步驟就是listen>accept>recieve,最好看看msdn,那個學起來才系統
3. 求winsocket編程的最簡例子,C++的
#include <winsock2.h>#pragma comment(lib,"WS2_32.lib")class CSockInit
{
public:
CSockInit(BYTE minorVer=2,BYTE majorVer=2)
{
WSADATA wsaData;
WORD sockVersion=MAKEWORD(minorVer,majorVer);
if(::WSAStartup(sockVersion,&wsaData)!=0)
{
exit(0);
}
}
~CSockInit()
{
::WSACleanup();
}
};//先定義個類原來初始化WinSocket //下面是伺服器端的的程序#include <stdio.h>
#include "CInitSock.h"CSockInit sockInit;
int main()
{
SOCKET sock=::socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(sock==INVALID_SOCKET)
{
printf("Failed socket\n");
return 0;
}
sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(4567);
sin.sin_addr.S_un.S_addr=INADDR_ANY;
if(::bind(sock,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)
{
printf("Failed bind\n");
return 0;
}
char buf[1024]={0};
sockaddr_in addr;
int nlen=sizeof(addr);
while (1)
{
int nRecv=recvfrom(sock,buf,1024,0,(sockaddr*)&addr,&nlen);
if(nRecv>0)
{
buf[nRecv]='\0';
printf("接收數據(%s):%s\n",::inet_ntoa(addr.sin_addr),buf);
}
}
::closesocket(sock);
} //下面是客服端的程序。 #include <stdio.h>
#include "..\CInitSock.h"
CSockInit sockin;
void main()
{
SOCKET s=::socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(s==INVALID_SOCKET)
{
printf("Failed socket()\n");
return ;
}
sockaddr_in addr;
addr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addr.sin_family=AF_INET;
addr.sin_port=htons(4567);
char zstext[]="TCP server Demo!\r\n";
while (1)
{
::sendto(s,zstext,strlen(zstext),0,(sockaddr*)&addr,sizeof(addr));
scanf("%s",zstext);
}
} 這個是基於UDP/IP的。水平有點差,莫見怪,在win32平台上,Visual studio 上基本上都能運行,不用鏈接靜態庫的,只要你包含了第一個封裝好了的頭文件就可以了。