|
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winsock.h>
#pragma comment (lib, "ws2_32.lib")
void main () // (int argc, char * argv [])
{
/ * // tranclate arguments
///////////////////////////////////////////////////
char * _port;
char * address;
char * path;
u_short port;
if (argc == 3)
{
_port = argv [1];
sscanf (_port, "% d",&port);
printf ("% d", port);
scanf ("% s");
address = argv [2];
path = argv [3];
} * /
// Init a winsock
///////////////////////////////////////////////////
int err;
WSADATA wsaData;
WORD m_wVersion = MAKEWORD (2,2);
if (err = WSAStartup (m_wVersion,&wsaData)! = 0)
{
printf ("Winsock Init Error.");
int err_WSA = WSAGetLastError ();
printf ("% d\n", err_WSA);
}
// Creat a socket
////////////////////////////////////////////////////
int server_socket;
server_socket = socket (AF_INET, SOCK_STREAM, 0);
if (server_socket == INVALID_SOCKET)
{
int err_sock = WSAGetLastError ();
printf ("Creat Socket Error!% d\n", err_sock);
}
// Bind Server
////////////////////////////////////////////////////
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons (2000);
// The Windows Sockets htons function converts a u_short
// from host to TCP / IP network byte order (which is big-endian).
//server.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
server.sin_addr.S_un.S_addr = inet_addr ("127.0.0.1");
if (bind (server_socket, (const sockaddr *)&server, sizeof (server)) == SOCKET_ERROR)
{
int err_bind = WSAGetLastError ();
printf ("Bind Socket Error!% d\n", err_bind);
}
if (listen (server_socket, 2) == SOCKET_ERROR)
{
int err_listen = WSAGetLastError ();
printf ("Listen Socket Error!% d\n", err_listen);
}
struct sockaddr_in client_address;
int client_len;
char * buff = (char *) malloc (1024);
int client_socket;
int readbytes;
int sendbytes;
Ranch
FILE * stream;
if ((stream = fopen ("17-Ono Lisa-Yesterday.mp3", "a + b")) == NULL)
{
int err_fopen = WSAGetLastError ();
printf ("Open file Error!% d", err_fopen);
}
while (1)
{
client_len = sizeof (client_address);
if ((client_socket = accept (server_socket, (struct sockaddr *)&client_address,&client_len)) == INVALID_SOCKET)
{
int err_acc = WSAGetLastError ();
printf ("accept Error!% d\n", err_acc);
}
Ranch
while ((readbytes = fread (buff, 1,1024, stream))> 0)
{
sendbytes = send (client_socket, buff, readbytes, 0);
if (sendbytes> = 0)
printf ("send% d bytes", sendbytes);
else
printf ("Send Data Error!");
}
printf ("% d", readbytes);
closesocket (client_socket);
}
} |
|