导航:首页 > 编程语言 > socket编程实例代码

socket编程实例代码

发布时间:2023-07-04 10:28:04

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 上基本上都能运行,不用链接静态库的,只要你包含了第一个封装好了的头文件就可以了。

阅读全文

与socket编程实例代码相关的资料

热点内容
安卓手机电话簿怎么导出到苹果手机 浏览:761
php实现投票 浏览:331
手机爆力解压加密视频文件 浏览:930
东方财富app怎么看北上资金图解 浏览:416
邢昭林程序员那么可爱拍现场 浏览:169
安卓什么应用可以免费看电视剧 浏览:504
合适pdf 浏览:293
app监测睡眠怎么选择 浏览:642
老人家用什么安卓手机好 浏览:955
解压包能不能送女朋友 浏览:701
好看发卡网源码 浏览:51
水平集算法matlab 浏览:769
局域网如何用ftp服务器配置 浏览:74
程序员惯性思考模式 浏览:441
如何在个税app上查身份证号 浏览:7
电视家app安装在电视上怎么安 浏览:889
怎么将pdf格式转化为图片格式 浏览:639
服务器拔掉raid卡怎么装系统 浏览:234
区域对称加密算法 浏览:247
数字转汉字php 浏览:735