linux socket网络编程怎样收发包 linux下socket 网络编程(客户端向服务器端发送文件...

linux\u4e0bC\u8bed\u8a00\u7528socket\u7f51\u7edc\u7f16\u7a0b\u600e\u4e48\u8ba1\u7b97\u4f20\u8f93\u901f\u5ea6\uff1f

\u8fd9\u8981\u4f60\u7684\u901a\u4fe1\u7a0b\u5e8f\u534f\u5546\u4e00\u4e2a\u534f\u8bae\uff0c\u6bd4\u5982\u5b9a\u4e49\u4e00\u4e2a\u901a\u4fe1\u7ed3\u6784\u4f53\uff0c\u4f20\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4e00\u5f00\u59cb\u53d1\u9001\u7ed3\u6784\u4f53\u7684\u4fe1\u606f\u8fc7\u53bb\uff0c\u544a\u8bc9\u5bf9\u7aef\u4f60\u7684\u6587\u4ef6\u603b\u5927\u5c0f\uff0c\u7136\u540e\uff0c\u4f20\u8f93\u8fc7\u7a0b\u4e2d\uff0c\u7edf\u8ba1\u5df2\u7ecf\u6536\u5230\u6216\u8005\u53d1\u9001\u7684\u6570\u636e\uff0c\u505a\u4e2a\u9664\u6cd5\u5c31\u5f97\u5230\u901f\u7387\u4e86\u3002

\u5177\u4f53\u8fd9\u7c7b\u534f\u5546\uff0c\u4f60\u53ef\u4ee5\u81ea\u5df1\u968f\u4fbf\u60f3\uff0c\u4e5f\u53ef\u4ee5\u501f\u9274\u73b0\u6709\u7684\u6bd4\u8f83\u597d\u7684\u4e00\u4e9b\u8bbe\u8ba1\uff0c\u6709\u4e9b\u8003\u8651\u65ad\u70b9\u7eed\u4f20\u7684\u6280\u672f\uff0c\u8fd8\u6709\u538b\u7f29\u7684\uff0c\u770b\u4f60\u4ee3\u7801\u4e5f\u4e0d\u9700\u8981\u8003\u8651\u5427\u3002

\u6e90\u4ee3\u7801\u5949\u4e0a\uff0c\u6d41\u7a0b\u56fe\u3002\u3002\u3002\u8fd9\u4e2a\u592a\u7b80\u5355\u4e86\uff0c\u4f60\u81ea\u5df1\u770b\u770b\u3002\u3002\u3002\u3002\u3002\u3002\u3002

//TCP
//\u670d\u52a1\u5668\u7aef\u7a0b\u5e8f
#include
#include
#include
#include
#include

#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 ) )//\u521d\u59cb\u5316
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}

if( ( iServerSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "\u521b\u5efa\u5957\u63a5\u5b57\u5931\u8d25!\n" );
WSACleanup( );
exit( 0 );
}

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//\u76d1\u89c6\u7684\u7aef\u53e3\u53f7
ServerAddr.sin_addr.s_addr = INADDR_ANY;//\u672c\u5730IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );


if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind\u8c03\u7528\u5931\u8d25!\n" );
WSACleanup( );
exit( 0 );
}

if( listen( iServerSock, BACKLOG ) == -1 )
{
printf( "listen\u8c03\u7528\u5931\u8d25!\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\u8c03\u7528\u5931\u8d25!\n" );
WSACleanup( );
exit( 0 );
}

printf( "\u670d\u52a1\u5668\u8fde\u63a5\u5230%s\n", inet_ntoa( ClientAddr.sin_addr ) );
if( send( iClientSock, buf, strlen( buf ), 0 ) == -1 )
{
printf( "send\u8c03\u7528\u5931\u8d25!" );
closesocket( iClientSock );
WSACleanup( );
exit( 0 );
}
}
}


/////\u5ba2\u6237\u7aef\u7a0b\u5e8f
#include
#include
#include
#include
#include

#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\u8c03\u7528\u5931\u8d25!" );
WSACleanup( );
exit( 0 );
}
*/


if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//\u521d\u59cb\u5316
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}

if( ( iClientSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "\u521b\u5efa\u5957\u63a5\u5b57\u5931\u8d25!\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" );//\u8bb0\u5f97\u6362IP
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );

if( connect( iClientSock, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "connect\u5931\u8d25!" );
WSACleanup( );
exit( 0 );
}

numbytes = recv( iClientSock, buf, MAXDATASIZE, 0 );

if( numbytes == -1 )
{
printf( "recv\u5931\u8d25!" );
WSACleanup( );
exit( 0 );
}

buf[ numbytes ] = '\0';

printf( "Received: %s", buf );

closesocket( iClientSock );
WSACleanup( );
}


/////UDP
//\u670d\u52a1\u5668
#include
#include
#include
#include

#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( "\u521b\u5efa\u5957\u63a5\u5b57\u5931\u8d25!\n" );
WSACleanup( );
exit( 0 );
}


ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//\u76d1\u89c6\u7684\u7aef\u53e3\u53f7
ServerAddr.sin_addr.s_addr = INADDR_ANY;//\u672c\u5730IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );


if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind\u8c03\u7528\u5931\u8d25!\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\u8c03\u7528\u5931\u8d25!\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( );


}
//\u5ba2\u6237\u7aef
#include
#include
#include
#include
#include

#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( "\u521b\u5efa\u5957\u63a5\u5b57\u5931\u8d25!\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" );//\u8bb0\u5f97\u6362IP
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\u8c03\u7528\u5931\u8d25!\n" );
WSACleanup( );
exit( 0 );
}

printf( "sent %d bytes to %s\n", numbytes, inet_ntoa( ServerAddr.sin_addr ) );

closesocket( iClientSock );
WSACleanup( );
}

1.send函数
ssize_t send( SOCKET s, const char *buf, size_t len, int flags );
(1)send先比较待发送数据的长度len和套接字s的发送缓冲的长度, 如果len大于s的发送缓冲区的长度,该函数返回SOCKET_ERROR;
(2)如果len小于或者等于s的发送缓冲区的长度,那么send先检查协议是否正在发送s的发送缓冲中的数据,如果是就等待协议把数据发送完,如果协议还没有开始发送s的发送缓冲中的数据或者s的发送缓冲中没有数据,那么send就比较s的发送缓冲区的剩余空间和len;
(3)如果len大于剩余空间大小,send就一直等待协议把s的发送缓冲中的数据发送完;
(4)如果len小于剩余空间大小,send就仅仅把buf中的数据copy到剩余空间里(注意并不是send把s的发送缓冲中的数据传到连接的另一端的,而是协议传送的,send仅仅是把buf中的数据copy到s的发送缓冲区的剩余空间里)。
注意:
(1)如果send函数copy数据成功,就返回实际copy的字节数,如果send在copy数据时出现错误,那么send就返回SOCKET_ERROR;如果send在等待协议传送数据时网络断开的话,那么send函数也返回SOCKET_ERROR。
(2)要注意send函数把buf中的数据成功copy到s的发送缓冲的剩余空间里后它就返回了,但是此时这些数据并不一定马上被传到连接的另一端。如果协议在后续的传送过程中出现网络错误的话,那么下一个Socket函数就会返回SOCKET_ERROR。(每一个除send外的Socket函数在执行的最开始总要先等待套接字的发送缓冲中的数据被协议传送完毕才能继续,如果在等待时出现网络错误,那么该Socket函数就返回 SOCKET_ERROR)
(3)在Unix系统下,如果send在等待协议传送数据时网络断开的话,调用send的进程会接收到一个SIGPIPE信号,进程对该信号的默认处理是进程终止。
2.recv函数
ssize_t recv(int s, char *buf, size_t len, int flags);
(1)recv先等待s的发送缓冲中的数据被协议传送完毕,如果协议在传送s的发送缓冲
中的数据时出现网络错误,那么recv函数返回SOCKET_ERROR。
(2)如果s的发送缓冲中没有数据或者数据被协议成功发送完毕后,recv先检查套接字s的接收缓冲区,如果s接收缓冲区中没有数据或者协议正在接收数 据,那么recv就一直等待,直到协议把数据接收完毕。当协议把数据接收完毕,recv函数就把s的接收缓冲中的数据copy到buf中。(注意:协议接收到的数据可能大于buf的长度,所以 在这种情况下要调用几次recv函数才能把s的接收缓冲中的数据copy完。recv函数仅仅是copy数据,真正的接收数据是协议来完成的)
(3)recv函数返回其实际copy的字节数。如果recv在copy时出错,那么它返回SOCKET_ERROR;如果recv函数在等待协议接收数据时网络中断了,那么它返回0。
注意:在Unix系统下,如果recv函数在等待协议接收数据时网络断开了,那么调用recv的进程会接收到一个SIGPIPE信号,进程对该信号的默认处理是进程终止。
Q&A:
(1)两次send一次recv会发生什么?
一次性读取两次send的内容。
(2)recv之后,接收缓冲区会被清空吗?
是的。

扩展阅读:免费网站入口在哪 ... linsoc下载app ... 国外永久免费云服务器 ... tracker地址更新网站 ... cdrx8永久禁止联网 ... linux kernel ... 私人网站服务器免费 ... 服务器节点购买 ... linux socket unix域 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网