linux & window socket programming (linux server, windows client)
I wanna connect linux server and windows client
These are simple(basic) example of linux server and windows client
These code work well in (linux server, linux client) and (windows server, window client)
(becuase, is it local ??)
How can I connect linux server and windows client??
(Case of linux, I executed codes in VirtualBox)
If client enter with port 55000, IP 127.0.0.1, they interaction, and change
some messages
client.c
(windows)
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <stdio.h>
#define PORT 55000
#define BUFFER_SIZE 100
int main() {
WSADATA wsdata;
if (WSAStartup(MAKEWORD(2, 2), &wsdata) != 0) {
printf("Fail to initiate ws2_d2.dll nn");
return 0;
}
printf("Success to initiate ws_32.dllnn");
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN serverAddress;
ZeroMemory(&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT);
****************//VirtualBox IP '10.0.2.15'********************
serverAddress.sin_addr.s_addr = inet_addr("10.0.2.15");
if ((connect(clientSocket, (SOCKADDR*)(&serverAddress), sizeof(serverAddress)) != 0)) {
printf("Fail to connect.nn");
return 0;
}
printf("connected!!! nn");
char toServer[BUFFER_SIZE];
strcpy(toServer, "Hello Server!!! I'm client!");
send(clientSocket, toServer, strlen(toServer) + 1, 0);
printf("Data to server : %snn", toServer);
char fromServer[BUFFER_SIZE + 1];
recv(clientSocket, fromServer, BUFFER_SIZE, 0);
printf("received data from server : %sn", fromServer);
closesocket(clientSocket);
WSACleanup();
printf("Exit ws_32.dllnn");
return 0;
}
server.c
(linux)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#define PORT 55000
int main(){
int server_socket;
int client_socket;
struct sockaddr_in server_address={0,};
struct sockaddr_in client_address={0,};
int client_address_size;
char toClient = "Hello Client!!n";
char fromClient[100];
server_socket = socket(AF_INET, SOCK_STREAM, 0);
printf("Server Socket Create!!!n");
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(PORT);
bind(server_socket,(struct sockaddr*)&server_address,
sizeof(server_address));
listen(server_socket, 5);
printf("Wait Client...n");
client_address_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr*)%client_address
&client_address_size)
printf("Client Connect!!!n");
read(client_socket, fromClient, sizeof(fromClient));
printf("From Client Message: %sn", fromClient);
write(client_socket, toClient, sizeof(toClient));
printf("To Client Message: %sn", toClient);
close(client_socket);
return 0;
}
linux windows sockets
|
show 1 more comment
I wanna connect linux server and windows client
These are simple(basic) example of linux server and windows client
These code work well in (linux server, linux client) and (windows server, window client)
(becuase, is it local ??)
How can I connect linux server and windows client??
(Case of linux, I executed codes in VirtualBox)
If client enter with port 55000, IP 127.0.0.1, they interaction, and change
some messages
client.c
(windows)
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <stdio.h>
#define PORT 55000
#define BUFFER_SIZE 100
int main() {
WSADATA wsdata;
if (WSAStartup(MAKEWORD(2, 2), &wsdata) != 0) {
printf("Fail to initiate ws2_d2.dll nn");
return 0;
}
printf("Success to initiate ws_32.dllnn");
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN serverAddress;
ZeroMemory(&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT);
****************//VirtualBox IP '10.0.2.15'********************
serverAddress.sin_addr.s_addr = inet_addr("10.0.2.15");
if ((connect(clientSocket, (SOCKADDR*)(&serverAddress), sizeof(serverAddress)) != 0)) {
printf("Fail to connect.nn");
return 0;
}
printf("connected!!! nn");
char toServer[BUFFER_SIZE];
strcpy(toServer, "Hello Server!!! I'm client!");
send(clientSocket, toServer, strlen(toServer) + 1, 0);
printf("Data to server : %snn", toServer);
char fromServer[BUFFER_SIZE + 1];
recv(clientSocket, fromServer, BUFFER_SIZE, 0);
printf("received data from server : %sn", fromServer);
closesocket(clientSocket);
WSACleanup();
printf("Exit ws_32.dllnn");
return 0;
}
server.c
(linux)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#define PORT 55000
int main(){
int server_socket;
int client_socket;
struct sockaddr_in server_address={0,};
struct sockaddr_in client_address={0,};
int client_address_size;
char toClient = "Hello Client!!n";
char fromClient[100];
server_socket = socket(AF_INET, SOCK_STREAM, 0);
printf("Server Socket Create!!!n");
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(PORT);
bind(server_socket,(struct sockaddr*)&server_address,
sizeof(server_address));
listen(server_socket, 5);
printf("Wait Client...n");
client_address_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr*)%client_address
&client_address_size)
printf("Client Connect!!!n");
read(client_socket, fromClient, sizeof(fromClient));
printf("From Client Message: %sn", fromClient);
write(client_socket, toClient, sizeof(toClient));
printf("To Client Message: %sn", toClient);
close(client_socket);
return 0;
}
linux windows sockets
1
Have you tried with the correct IP address? 127.0.0.1 is the loopback address which points to the machine itself. There’s usually nothing in the code itself that wouldn’t work across machines if it works within one.
– Sami Kuhmonen
Nov 24 '18 at 14:46
Thansk! so, will ti be work to change '127.0.0.1' to 'VirtualBox's IP' in the code 'serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");'?
– Kim Sunwoo
Nov 24 '18 at 14:59
and, additional environment setting is needed?
– Kim Sunwoo
Nov 24 '18 at 15:01
Yes, change that to point to the server’s IP address. Other changes should not be needed
– Sami Kuhmonen
Nov 24 '18 at 15:02
Hmm.. I changed IP to 'VirtualBox IP '10.0.2.15'. but it didn't work! Is there anything what I don't understand?
– Kim Sunwoo
Nov 24 '18 at 15:23
|
show 1 more comment
I wanna connect linux server and windows client
These are simple(basic) example of linux server and windows client
These code work well in (linux server, linux client) and (windows server, window client)
(becuase, is it local ??)
How can I connect linux server and windows client??
(Case of linux, I executed codes in VirtualBox)
If client enter with port 55000, IP 127.0.0.1, they interaction, and change
some messages
client.c
(windows)
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <stdio.h>
#define PORT 55000
#define BUFFER_SIZE 100
int main() {
WSADATA wsdata;
if (WSAStartup(MAKEWORD(2, 2), &wsdata) != 0) {
printf("Fail to initiate ws2_d2.dll nn");
return 0;
}
printf("Success to initiate ws_32.dllnn");
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN serverAddress;
ZeroMemory(&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT);
****************//VirtualBox IP '10.0.2.15'********************
serverAddress.sin_addr.s_addr = inet_addr("10.0.2.15");
if ((connect(clientSocket, (SOCKADDR*)(&serverAddress), sizeof(serverAddress)) != 0)) {
printf("Fail to connect.nn");
return 0;
}
printf("connected!!! nn");
char toServer[BUFFER_SIZE];
strcpy(toServer, "Hello Server!!! I'm client!");
send(clientSocket, toServer, strlen(toServer) + 1, 0);
printf("Data to server : %snn", toServer);
char fromServer[BUFFER_SIZE + 1];
recv(clientSocket, fromServer, BUFFER_SIZE, 0);
printf("received data from server : %sn", fromServer);
closesocket(clientSocket);
WSACleanup();
printf("Exit ws_32.dllnn");
return 0;
}
server.c
(linux)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#define PORT 55000
int main(){
int server_socket;
int client_socket;
struct sockaddr_in server_address={0,};
struct sockaddr_in client_address={0,};
int client_address_size;
char toClient = "Hello Client!!n";
char fromClient[100];
server_socket = socket(AF_INET, SOCK_STREAM, 0);
printf("Server Socket Create!!!n");
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(PORT);
bind(server_socket,(struct sockaddr*)&server_address,
sizeof(server_address));
listen(server_socket, 5);
printf("Wait Client...n");
client_address_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr*)%client_address
&client_address_size)
printf("Client Connect!!!n");
read(client_socket, fromClient, sizeof(fromClient));
printf("From Client Message: %sn", fromClient);
write(client_socket, toClient, sizeof(toClient));
printf("To Client Message: %sn", toClient);
close(client_socket);
return 0;
}
linux windows sockets
I wanna connect linux server and windows client
These are simple(basic) example of linux server and windows client
These code work well in (linux server, linux client) and (windows server, window client)
(becuase, is it local ??)
How can I connect linux server and windows client??
(Case of linux, I executed codes in VirtualBox)
If client enter with port 55000, IP 127.0.0.1, they interaction, and change
some messages
client.c
(windows)
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <stdio.h>
#define PORT 55000
#define BUFFER_SIZE 100
int main() {
WSADATA wsdata;
if (WSAStartup(MAKEWORD(2, 2), &wsdata) != 0) {
printf("Fail to initiate ws2_d2.dll nn");
return 0;
}
printf("Success to initiate ws_32.dllnn");
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN serverAddress;
ZeroMemory(&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT);
****************//VirtualBox IP '10.0.2.15'********************
serverAddress.sin_addr.s_addr = inet_addr("10.0.2.15");
if ((connect(clientSocket, (SOCKADDR*)(&serverAddress), sizeof(serverAddress)) != 0)) {
printf("Fail to connect.nn");
return 0;
}
printf("connected!!! nn");
char toServer[BUFFER_SIZE];
strcpy(toServer, "Hello Server!!! I'm client!");
send(clientSocket, toServer, strlen(toServer) + 1, 0);
printf("Data to server : %snn", toServer);
char fromServer[BUFFER_SIZE + 1];
recv(clientSocket, fromServer, BUFFER_SIZE, 0);
printf("received data from server : %sn", fromServer);
closesocket(clientSocket);
WSACleanup();
printf("Exit ws_32.dllnn");
return 0;
}
server.c
(linux)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#define PORT 55000
int main(){
int server_socket;
int client_socket;
struct sockaddr_in server_address={0,};
struct sockaddr_in client_address={0,};
int client_address_size;
char toClient = "Hello Client!!n";
char fromClient[100];
server_socket = socket(AF_INET, SOCK_STREAM, 0);
printf("Server Socket Create!!!n");
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(PORT);
bind(server_socket,(struct sockaddr*)&server_address,
sizeof(server_address));
listen(server_socket, 5);
printf("Wait Client...n");
client_address_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr*)%client_address
&client_address_size)
printf("Client Connect!!!n");
read(client_socket, fromClient, sizeof(fromClient));
printf("From Client Message: %sn", fromClient);
write(client_socket, toClient, sizeof(toClient));
printf("To Client Message: %sn", toClient);
close(client_socket);
return 0;
}
linux windows sockets
linux windows sockets
edited Nov 24 '18 at 15:21
Kim Sunwoo
asked Nov 24 '18 at 14:18
Kim SunwooKim Sunwoo
64
64
1
Have you tried with the correct IP address? 127.0.0.1 is the loopback address which points to the machine itself. There’s usually nothing in the code itself that wouldn’t work across machines if it works within one.
– Sami Kuhmonen
Nov 24 '18 at 14:46
Thansk! so, will ti be work to change '127.0.0.1' to 'VirtualBox's IP' in the code 'serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");'?
– Kim Sunwoo
Nov 24 '18 at 14:59
and, additional environment setting is needed?
– Kim Sunwoo
Nov 24 '18 at 15:01
Yes, change that to point to the server’s IP address. Other changes should not be needed
– Sami Kuhmonen
Nov 24 '18 at 15:02
Hmm.. I changed IP to 'VirtualBox IP '10.0.2.15'. but it didn't work! Is there anything what I don't understand?
– Kim Sunwoo
Nov 24 '18 at 15:23
|
show 1 more comment
1
Have you tried with the correct IP address? 127.0.0.1 is the loopback address which points to the machine itself. There’s usually nothing in the code itself that wouldn’t work across machines if it works within one.
– Sami Kuhmonen
Nov 24 '18 at 14:46
Thansk! so, will ti be work to change '127.0.0.1' to 'VirtualBox's IP' in the code 'serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");'?
– Kim Sunwoo
Nov 24 '18 at 14:59
and, additional environment setting is needed?
– Kim Sunwoo
Nov 24 '18 at 15:01
Yes, change that to point to the server’s IP address. Other changes should not be needed
– Sami Kuhmonen
Nov 24 '18 at 15:02
Hmm.. I changed IP to 'VirtualBox IP '10.0.2.15'. but it didn't work! Is there anything what I don't understand?
– Kim Sunwoo
Nov 24 '18 at 15:23
1
1
Have you tried with the correct IP address? 127.0.0.1 is the loopback address which points to the machine itself. There’s usually nothing in the code itself that wouldn’t work across machines if it works within one.
– Sami Kuhmonen
Nov 24 '18 at 14:46
Have you tried with the correct IP address? 127.0.0.1 is the loopback address which points to the machine itself. There’s usually nothing in the code itself that wouldn’t work across machines if it works within one.
– Sami Kuhmonen
Nov 24 '18 at 14:46
Thansk! so, will ti be work to change '127.0.0.1' to 'VirtualBox's IP' in the code 'serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");'?
– Kim Sunwoo
Nov 24 '18 at 14:59
Thansk! so, will ti be work to change '127.0.0.1' to 'VirtualBox's IP' in the code 'serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");'?
– Kim Sunwoo
Nov 24 '18 at 14:59
and, additional environment setting is needed?
– Kim Sunwoo
Nov 24 '18 at 15:01
and, additional environment setting is needed?
– Kim Sunwoo
Nov 24 '18 at 15:01
Yes, change that to point to the server’s IP address. Other changes should not be needed
– Sami Kuhmonen
Nov 24 '18 at 15:02
Yes, change that to point to the server’s IP address. Other changes should not be needed
– Sami Kuhmonen
Nov 24 '18 at 15:02
Hmm.. I changed IP to 'VirtualBox IP '10.0.2.15'. but it didn't work! Is there anything what I don't understand?
– Kim Sunwoo
Nov 24 '18 at 15:23
Hmm.. I changed IP to 'VirtualBox IP '10.0.2.15'. but it didn't work! Is there anything what I don't understand?
– Kim Sunwoo
Nov 24 '18 at 15:23
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459075%2flinux-window-socket-programming-linux-server-windows-client%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459075%2flinux-window-socket-programming-linux-server-windows-client%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you tried with the correct IP address? 127.0.0.1 is the loopback address which points to the machine itself. There’s usually nothing in the code itself that wouldn’t work across machines if it works within one.
– Sami Kuhmonen
Nov 24 '18 at 14:46
Thansk! so, will ti be work to change '127.0.0.1' to 'VirtualBox's IP' in the code 'serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");'?
– Kim Sunwoo
Nov 24 '18 at 14:59
and, additional environment setting is needed?
– Kim Sunwoo
Nov 24 '18 at 15:01
Yes, change that to point to the server’s IP address. Other changes should not be needed
– Sami Kuhmonen
Nov 24 '18 at 15:02
Hmm.. I changed IP to 'VirtualBox IP '10.0.2.15'. but it didn't work! Is there anything what I don't understand?
– Kim Sunwoo
Nov 24 '18 at 15:23