| |

VerySource

 Forgot password?
 Register
Search
View: 639|Reply: 1

Which big brother helped port this program to Linux

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-9 09:30:01
| Show all posts |Read mode
#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);
}
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-10 05:00:01
| Show all posts
Remove things with WSA.
Add some header files
#include <netinet / in.h>
#include <sys / socket.h>
#include <arpa / inet.h>

Remove
#include <windows.h>
#include <winsock.h>
#pragma comment (lib, "ws2_32.lib")

.... Maybe there is something missing like sys / types.h unistd.h ... adjust the rest by yourself
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list