当前位置: 技术问答>linux和unix
linux socket -> windows
来源: 互联网 发布时间:2016-04-01
本文导语: 下面是一个Linux下的一段程序,要使得它能在windows下运行,要改哪些呀。 我是通过定义宏(主要是一些头文件,如下面的socket.h->winsock2.h)来做的,但好像不是那么简单 #include "stdio.h" #include "stdlib.h" #include "rsl.h" ...
下面是一个Linux下的一段程序,要使得它能在windows下运行,要改哪些呀。
我是通过定义宏(主要是一些头文件,如下面的socket.h->winsock2.h)来做的,但好像不是那么简单
#include "stdio.h"
#include "stdlib.h"
#include "rsl.h"
#include
#include
#include
#include
#include
RSL_SOCKCLOSE ( sid0 )
int_p sid0 ; /* socket id -- set by this routine */
{
int retval ;
RSL_C_IAMMONITOR ( &retval ) ;
if ( retval == 1 )
{
close(*sid0) ;
}
}
我是通过定义宏(主要是一些头文件,如下面的socket.h->winsock2.h)来做的,但好像不是那么简单
#include "stdio.h"
#include "stdlib.h"
#include "rsl.h"
#include
#include
#include
#include
#include
RSL_SOCKCLOSE ( sid0 )
int_p sid0 ; /* socket id -- set by this routine */
{
int retval ;
RSL_C_IAMMONITOR ( &retval ) ;
if ( retval == 1 )
{
close(*sid0) ;
}
}
|
首先要
#pragma comment//载入一个库
接着在main函数里要
WSStarup开启,查下MSDN
就可以了。其他一样
#pragma comment//载入一个库
接着在main函数里要
WSStarup开启,查下MSDN
就可以了。其他一样
|
先在windows下将一个windows版本的gcc,即MinGW。这样一些头文件就有了,然后你在windows下用gcc编译看看。
|
以下是我由linux下转换成windows下的一段程序,一个TCP/IP的client段,可以参考参考
// ftp_client.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/*******¿Í»§¶Ë³ÌÐò client.c*********/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define bzero(x,y) memset(x, 0, y)
int main(int argc, char *argv[])
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes;
if (argc!=3)
{
fprintf(stderr,"Usage:%s hostname portnumberan",argv[0]);
exit(1);
}
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
if ( 0 != WSAStartup( wVersionRequested, &wsaData ) ) //³õʼ»¯
{
exit(1) ;
}
if ((host=gethostbyname(argv[1]))==NULL)
{
fprintf(stderr,"Gethostname errorn");
DWORD dwError = WSAGetLastError() ;
exit(1);
}
if ((portnumber=atoi(argv[2]))h_addr);
/*¿Í»§³ÌÐò·¢ÆðÁ¬½ÓÇëÇó*/
if (connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Connect Error:%san",strerror(errno));
exit(1);
}
if ((nbytes=recv(sockfd,buffer,1024,0))==-1)
{
fprintf(stderr,"Read Error:%sn",strerror(errno));
exit(1);
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
closesocket(sockfd);
WSACleanup(); //ÊÍ·Å
return 0;
}
// ftp_client.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/*******¿Í»§¶Ë³ÌÐò client.c*********/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define bzero(x,y) memset(x, 0, y)
int main(int argc, char *argv[])
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes;
if (argc!=3)
{
fprintf(stderr,"Usage:%s hostname portnumberan",argv[0]);
exit(1);
}
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
if ( 0 != WSAStartup( wVersionRequested, &wsaData ) ) //³õʼ»¯
{
exit(1) ;
}
if ((host=gethostbyname(argv[1]))==NULL)
{
fprintf(stderr,"Gethostname errorn");
DWORD dwError = WSAGetLastError() ;
exit(1);
}
if ((portnumber=atoi(argv[2]))h_addr);
/*¿Í»§³ÌÐò·¢ÆðÁ¬½ÓÇëÇó*/
if (connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Connect Error:%san",strerror(errno));
exit(1);
}
if ((nbytes=recv(sockfd,buffer,1024,0))==-1)
{
fprintf(stderr,"Read Error:%sn",strerror(errno));
exit(1);
}
buffer[nbytes]='';
printf("I have received:%sn",buffer);
closesocket(sockfd);
WSACleanup(); //ÊÍ·Å
return 0;
}
|
最好用minGW,这些编出来的程序是标准的windows程序。
而cygwin编写出来的,要带一些很大的dll文件才能运行
而cygwin编写出来的,要带一些很大的dll文件才能运行
|
这个好像改动比较大,,
首先,头文件不一样,,,
linux下不需要引用dll文件,而windows需要,如:ws_32.dll(好像是这个,记不清了,长的跟这个差不多,呵呵)
建立socket,windows需要WSAStart,,而linux/unix下不需要...linux/unix下也没有WSAEcleanUp(),,
其它的一些socket函数用法都差不多..
首先,头文件不一样,,,
linux下不需要引用dll文件,而windows需要,如:ws_32.dll(好像是这个,记不清了,长的跟这个差不多,呵呵)
建立socket,windows需要WSAStart,,而linux/unix下不需要...linux/unix下也没有WSAEcleanUp(),,
其它的一些socket函数用法都差不多..
|
这个些的还不错!
http://tangentsoft.net/wskfaq/articles/bsd-compatibility.html
http://tangentsoft.net/wskfaq/articles/bsd-compatibility.html