当前位置: 技术问答>linux和unix
一个新手,请教一个将linux下编的C程序转换到window下的问题
来源: 互联网 发布时间:2015-07-10
本文导语: #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int sockfd,new_fd; int whilen; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber;...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int sockfd,new_fd;
int whilen;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size,portnumber;
int nbytes,totalbytes;
int i;
char hello[]="Hello! Are You Fine?n";
struct timeval tpstart,tpend;
float timeuse;
whilen=10;
timeuse=0;
totalbytes=0;
nbytes=0;
if(argc!=2)//argc not have a vaule at before,so why argc!=2??
{
fprintf(stderr,"Usage:%s portnumberan",argv[0]);
exit(1);
}
if((portnumber=atoi(argv[1]))0)
{
totalbytes+=nbytes;
//measure time
timeuse+=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
tpstart=tpend;
gettimeofday(&tpend,NULL);
}
whilen--;
}
printf("the down rate %f Kbps,total throughput %d bytesn",totalbytes*8/1000/timeuse,totalbytes);
close(new_fd);
close(sockfd);
exit(0);
}
我这里并不希望大家能完全给我答案,当然这也是不可能的,我只是想请各位大虾高人能告诉我这些在程序中用到的函数的功能以及在windows下的对应函数(包括开头的那些头文件),不指望一个网友能解决我所有的问题,众人拾柴火焰高,每位网友都帮俺一点啊!在此十分感谢!
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int sockfd,new_fd;
int whilen;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size,portnumber;
int nbytes,totalbytes;
int i;
char hello[]="Hello! Are You Fine?n";
struct timeval tpstart,tpend;
float timeuse;
whilen=10;
timeuse=0;
totalbytes=0;
nbytes=0;
if(argc!=2)//argc not have a vaule at before,so why argc!=2??
{
fprintf(stderr,"Usage:%s portnumberan",argv[0]);
exit(1);
}
if((portnumber=atoi(argv[1]))0)
{
totalbytes+=nbytes;
//measure time
timeuse+=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
tpstart=tpend;
gettimeofday(&tpend,NULL);
}
whilen--;
}
printf("the down rate %f Kbps,total throughput %d bytesn",totalbytes*8/1000/timeuse,totalbytes);
close(new_fd);
close(sockfd);
exit(0);
}
我这里并不希望大家能完全给我答案,当然这也是不可能的,我只是想请各位大虾高人能告诉我这些在程序中用到的函数的功能以及在windows下的对应函数(包括开头的那些头文件),不指望一个网友能解决我所有的问题,众人拾柴火焰高,每位网友都帮俺一点啊!在此十分感谢!
|
基本上对应的操作系统接口得换。 socket函数到winsock函数,具体的要查msdn。
|
/*--
Copyright (c) 2003 Shenzhen Huaren Education Co.Ltd
File Name:
bsocket.h
Version:
2.0
Abstract:
Definitions for portable Berkeley socket operation.
Author:
Gang He
Created on:
2003-11-12
Modified History:
2004-06-23
Modified Person:
Gang He
--*/
#ifndef _HEGANG_BSOCKET_H_
#define _HEGANG_BSOCKET_H_
#ifdef WIN32
#include
typedef int socklen_t;
#else
#include
#include
#include
#include
#include
#include
typedef int SOCKET;
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#endif
typedef unsigned short PORT;
#define SOCKET_TIMEOUT -2
#ifdef __cplusplus
extern "C" {
#endif
/*-
Name:
SOCKET create_sock(int domain, int type, int protocol)
Description:
Create a socket descriptor
Input Parameters:
domain: the protocol family
type: socket type
protocol: a particular protocol
Output Parameters:
nothing
Return Value:
a socket descriptor, otherwise INVALID_SOCKET is returned if an error occurred.
-*/
SOCKET create_sock(int domain, int type, int protocol);
/*-
Name:
int bind_sock(SOCKET sock, PORT port)
Description:
Bind a socket
Input Parameters:
sock: socket descriptor
port: port to be binded
Output Parameters:
nothing
Return Value:
0, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int bind_sock(SOCKET sock, PORT port);
/*-
Name:
int listen_sock(SOCKET sock, int backlog)
Description:
Listen a socket
Input Parameters:
sock: socket descriptor
backlog: listen queue length
Output Parameters:
nothing
Return Value:
0, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int listen_sock(SOCKET sock, int backlog);
/*-
Name:
SOCKET accept_sock(SOCKET sock, char* fromip)
Description:
Accept a socket
Input Parameters:
sock: socket descriptor
Output Parameters:
fromip: peer IP address, this buffer must be larger or equal to 16 bytes,
otherwise, set NULL.
Return Value:
a socket descriptor, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
SOCKET accept_sock(SOCKET sock, char* fromip);
/*-
Name:
int connect_sock(SOCKET sock, const char* ip, PORT port)
Description:
Connect a socket
Input Parameters:
sock: socket descriptor
ip: peer IP address
port: peer port
Output Parameters:
nothing
Return Value:
0, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int connect_sock(SOCKET sock, const char* ip, PORT port);
/*-
Name:
int send_sock(SOCKET sock, const void* buf, int len)
Description:
Send data by socket
Input Parameters:
sock: socket descriptor
buf: data buffer
len: data size
Output Parameters:
nothing
Return Value:
the number of bytes sent, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int send_sock(SOCKET sock, const void* buf, int len);
/*-
Name:
int sendto_sock(SOCKET sock, const void* buf, int len, const char* ip, PORT port)
Description:
Send data message by socket
Input Parameters:
sock: socket descriptor
buf: message buffer
len: message size
ip: peer ip address
port: peer port
Output Parameters:
nothing
Return Value:
the number of bytes sent, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int sendto_sock(SOCKET sock, const void* buf, int len, const char* ip, PORT port);
/*-
Name:
int recv_sock(SOCKET sock, void* buf, int len)
Description:
Receive data from a socket
Input Parameters:
sock: socket descriptor
Output Parameters:
buf: data buffer
len: buffer size
Return Value:
the number of bytes received, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int recv_sock(SOCKET sock, void* buf, int len);
/*-
Name:
int recvex_sock(SOCKET sock, void* buf, int len, int sec)
Description:
Receive data from a socket
Input Parameters:
sock: socket descriptor
sec: timeout time
Output Parameters:
buf: data buffer
len: buffer size
Return Value:
the number of bytes received, otherwise
SOCKET_ERROR is returned if an error occurred,
SOCKET_TIMEOUT is returned if no data be received.
-*/
int recvex_sock(SOCKET sock, void* buf, int len, int sec);
/*-
Name:
int recvfrom_sock(SOCKET sock, void* buf, int len, char* fromip, PORT* port)
Description:
Receive data message from a socket
Input Parameters:
sock: socket descriptor
Output Parameters:
buf: data message buffer
len: buffer size
fromip: peer ip address
port: peer port
Return Value:
the number of bytes received, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int recvfrom_sock(SOCKET sock, void* buf, int len, char* fromip, PORT* port);
/*-
Name:
void close_sock(SOCKET* sock)
Description:
Close a socket
Input Parameters:
sock: socket pointer
Output Parameters:
sock: set *sock INVALID_SOCKET
Return Value:
nothing
-*/
void close_sock(SOCKET* sock);
/*-
Name:
int init_sock()
Description:
Initialize winsock DLL
Input Parameters:
nothing
Output Parameters:
nothing
Return Value:
0, otherwise nonzero is returned if an error occurred.
-*/
#ifdef WIN32
int init_sock();
#endif
/*-
Name:
int clean_sock()
Description:
Clean up winsock DLL
Input Parameters:
nothing
Output Parameters:
nothing
Return Value:
0, otherwise nonzero is returned if an error occurred.
-*/
#ifdef WIN32
int clean_sock();
#endif
#ifdef __cplusplus
}
#endif
#endif
Copyright (c) 2003 Shenzhen Huaren Education Co.Ltd
File Name:
bsocket.h
Version:
2.0
Abstract:
Definitions for portable Berkeley socket operation.
Author:
Gang He
Created on:
2003-11-12
Modified History:
2004-06-23
Modified Person:
Gang He
--*/
#ifndef _HEGANG_BSOCKET_H_
#define _HEGANG_BSOCKET_H_
#ifdef WIN32
#include
typedef int socklen_t;
#else
#include
#include
#include
#include
#include
#include
typedef int SOCKET;
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#endif
typedef unsigned short PORT;
#define SOCKET_TIMEOUT -2
#ifdef __cplusplus
extern "C" {
#endif
/*-
Name:
SOCKET create_sock(int domain, int type, int protocol)
Description:
Create a socket descriptor
Input Parameters:
domain: the protocol family
type: socket type
protocol: a particular protocol
Output Parameters:
nothing
Return Value:
a socket descriptor, otherwise INVALID_SOCKET is returned if an error occurred.
-*/
SOCKET create_sock(int domain, int type, int protocol);
/*-
Name:
int bind_sock(SOCKET sock, PORT port)
Description:
Bind a socket
Input Parameters:
sock: socket descriptor
port: port to be binded
Output Parameters:
nothing
Return Value:
0, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int bind_sock(SOCKET sock, PORT port);
/*-
Name:
int listen_sock(SOCKET sock, int backlog)
Description:
Listen a socket
Input Parameters:
sock: socket descriptor
backlog: listen queue length
Output Parameters:
nothing
Return Value:
0, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int listen_sock(SOCKET sock, int backlog);
/*-
Name:
SOCKET accept_sock(SOCKET sock, char* fromip)
Description:
Accept a socket
Input Parameters:
sock: socket descriptor
Output Parameters:
fromip: peer IP address, this buffer must be larger or equal to 16 bytes,
otherwise, set NULL.
Return Value:
a socket descriptor, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
SOCKET accept_sock(SOCKET sock, char* fromip);
/*-
Name:
int connect_sock(SOCKET sock, const char* ip, PORT port)
Description:
Connect a socket
Input Parameters:
sock: socket descriptor
ip: peer IP address
port: peer port
Output Parameters:
nothing
Return Value:
0, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int connect_sock(SOCKET sock, const char* ip, PORT port);
/*-
Name:
int send_sock(SOCKET sock, const void* buf, int len)
Description:
Send data by socket
Input Parameters:
sock: socket descriptor
buf: data buffer
len: data size
Output Parameters:
nothing
Return Value:
the number of bytes sent, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int send_sock(SOCKET sock, const void* buf, int len);
/*-
Name:
int sendto_sock(SOCKET sock, const void* buf, int len, const char* ip, PORT port)
Description:
Send data message by socket
Input Parameters:
sock: socket descriptor
buf: message buffer
len: message size
ip: peer ip address
port: peer port
Output Parameters:
nothing
Return Value:
the number of bytes sent, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int sendto_sock(SOCKET sock, const void* buf, int len, const char* ip, PORT port);
/*-
Name:
int recv_sock(SOCKET sock, void* buf, int len)
Description:
Receive data from a socket
Input Parameters:
sock: socket descriptor
Output Parameters:
buf: data buffer
len: buffer size
Return Value:
the number of bytes received, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int recv_sock(SOCKET sock, void* buf, int len);
/*-
Name:
int recvex_sock(SOCKET sock, void* buf, int len, int sec)
Description:
Receive data from a socket
Input Parameters:
sock: socket descriptor
sec: timeout time
Output Parameters:
buf: data buffer
len: buffer size
Return Value:
the number of bytes received, otherwise
SOCKET_ERROR is returned if an error occurred,
SOCKET_TIMEOUT is returned if no data be received.
-*/
int recvex_sock(SOCKET sock, void* buf, int len, int sec);
/*-
Name:
int recvfrom_sock(SOCKET sock, void* buf, int len, char* fromip, PORT* port)
Description:
Receive data message from a socket
Input Parameters:
sock: socket descriptor
Output Parameters:
buf: data message buffer
len: buffer size
fromip: peer ip address
port: peer port
Return Value:
the number of bytes received, otherwise SOCKET_ERROR is returned if an error occurred.
-*/
int recvfrom_sock(SOCKET sock, void* buf, int len, char* fromip, PORT* port);
/*-
Name:
void close_sock(SOCKET* sock)
Description:
Close a socket
Input Parameters:
sock: socket pointer
Output Parameters:
sock: set *sock INVALID_SOCKET
Return Value:
nothing
-*/
void close_sock(SOCKET* sock);
/*-
Name:
int init_sock()
Description:
Initialize winsock DLL
Input Parameters:
nothing
Output Parameters:
nothing
Return Value:
0, otherwise nonzero is returned if an error occurred.
-*/
#ifdef WIN32
int init_sock();
#endif
/*-
Name:
int clean_sock()
Description:
Clean up winsock DLL
Input Parameters:
nothing
Output Parameters:
nothing
Return Value:
0, otherwise nonzero is returned if an error occurred.
-*/
#ifdef WIN32
int clean_sock();
#endif
#ifdef __cplusplus
}
#endif
#endif
|
在windows上记得要用WSAStartup()和WSACleanup()
|
楼上可谓俯首干为楼主牛阿。^_^
|
看看stevens的unp吧,解释太清楚了
|
winsock这一套嘛,都有和linux下面socket相对应的
|
up
|
mark
|
不会太难