TCP Client Server BSD Socket c++









up vote
-1
down vote

favorite
1












I wanna make 3 programms- client, server and dotmaker.
In this case when i started in 2 terminal in first ./server, in second ./client and in client i wrote for example "abcd" i got just once "a.b.c.d", after this my server is not respoding.
I want enter a many messages and get a dotted message every time.



i got this:



./server
Czekam....
Connection accepted from 0.208.246.183:62647
g.o.o.d.b.y.e.

./client
Connected to Server.
Client: goodbye
Server: goodbye
Client: goodbye2


Here is the code



dot.cc



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 4444

#include <iostream>
using namespace std;

int main(int argc, char *argv)


string c="";
c=argv[1];

for(int i=0;i<strlen(argv[1]);i++)

cout<<c[i]<<".";

cout<<endl;

return 0;



client



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 4444

int main()

int clientSocket, i;
struct sockaddr_in serverAddr;
char buffer[1024];

clientSocket = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddr, '', sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

i = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
printf("Connected to Server.n");

while(1)
printf("Client: t");
scanf("%s", &buffer[0]);
send(clientSocket, buffer, strlen(buffer),0);
if(sizeof(buffer)==0)
printf("Disconnected from server.n");
close(clientSocket);
exit(1);

recv(clientSocket, buffer,1024,0);
printf("Server: t%sn", buffer);



return 0;



server.cc



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#define PORT 4444
#include <iostream>
using namespace std;
int main()

socklen_t addr_size;
char buffer[1024];
pid_t childpid;

int newSocket,status;
struct sockaddr_in newAddr;

int sock,i,j;
struct sockaddr_in serverAddr;

sock = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

i= bind(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr));

listen(sock, 10);
printf("Czekam....n");

while(1)
newSocket = accept(sock, (struct sockaddr*)&newAddr, &addr_size);
if(newSocket < 0)
exit(1);

printf("Connection accepted from %s:%dn", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
if((childpid = fork()) == 0)
close(sock);
while(1)

if(recv(newSocket, buffer, 1024,0)==0)
printf("Disconnected from %s:%dn", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
break;
else
sleep(1);
send(newSocket,buffer,strlen(buffer),0);
execlp("./kropka","./kropka",buffer,NULL);
exit(1);



else
wait(NULL);



close(newSocket);

return 0;










share|improve this question





















  • why do you need to fork?
    – OznOg
    Nov 10 at 20:15






  • 2




    Before you start writing any code, write down a specification for the protocol your server and client will use to exchange data. Otherwise, it will be impossible to determine what to fix if the server and client don't work. Is the server wrong? Is the client wrong? How can you tell if you don't have a specification to compare them both to? If you're going to exchange messages, you need a message protocol. TCP is not a message protocol. So you'll have to build one on top of TCP.
    – David Schwartz
    Nov 10 at 20:26











  • my server need to service a lot of clients
    – owczar95
    Nov 10 at 20:27










  • the main problem of this programm is execlp, without it everything working well. I mean instead of line in server execlp("./kropka","./kropka",buffer,NULL) use " cout<<buffer<<endl; "
    – owczar95
    Nov 10 at 20:32






  • 1




    @owczar95 Unfortunately, your code is working largely by luck. This makes it hard to find and identify the various problems because they aren't causing your code to fail. At a minimum, you absolutely must write functions to send and receive messages because your application requires you to be able to send and receive messages and nowhere do you have code that has any ability to do that. TCP is not a message protocol. You can build a message protocol on top of TCP, but you have to actually write code to do that.
    – David Schwartz
    Nov 10 at 20:35















up vote
-1
down vote

favorite
1












I wanna make 3 programms- client, server and dotmaker.
In this case when i started in 2 terminal in first ./server, in second ./client and in client i wrote for example "abcd" i got just once "a.b.c.d", after this my server is not respoding.
I want enter a many messages and get a dotted message every time.



i got this:



./server
Czekam....
Connection accepted from 0.208.246.183:62647
g.o.o.d.b.y.e.

./client
Connected to Server.
Client: goodbye
Server: goodbye
Client: goodbye2


Here is the code



dot.cc



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 4444

#include <iostream>
using namespace std;

int main(int argc, char *argv)


string c="";
c=argv[1];

for(int i=0;i<strlen(argv[1]);i++)

cout<<c[i]<<".";

cout<<endl;

return 0;



client



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 4444

int main()

int clientSocket, i;
struct sockaddr_in serverAddr;
char buffer[1024];

clientSocket = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddr, '', sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

i = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
printf("Connected to Server.n");

while(1)
printf("Client: t");
scanf("%s", &buffer[0]);
send(clientSocket, buffer, strlen(buffer),0);
if(sizeof(buffer)==0)
printf("Disconnected from server.n");
close(clientSocket);
exit(1);

recv(clientSocket, buffer,1024,0);
printf("Server: t%sn", buffer);



return 0;



server.cc



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#define PORT 4444
#include <iostream>
using namespace std;
int main()

socklen_t addr_size;
char buffer[1024];
pid_t childpid;

int newSocket,status;
struct sockaddr_in newAddr;

int sock,i,j;
struct sockaddr_in serverAddr;

sock = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

i= bind(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr));

listen(sock, 10);
printf("Czekam....n");

while(1)
newSocket = accept(sock, (struct sockaddr*)&newAddr, &addr_size);
if(newSocket < 0)
exit(1);

printf("Connection accepted from %s:%dn", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
if((childpid = fork()) == 0)
close(sock);
while(1)

if(recv(newSocket, buffer, 1024,0)==0)
printf("Disconnected from %s:%dn", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
break;
else
sleep(1);
send(newSocket,buffer,strlen(buffer),0);
execlp("./kropka","./kropka",buffer,NULL);
exit(1);



else
wait(NULL);



close(newSocket);

return 0;










share|improve this question





















  • why do you need to fork?
    – OznOg
    Nov 10 at 20:15






  • 2




    Before you start writing any code, write down a specification for the protocol your server and client will use to exchange data. Otherwise, it will be impossible to determine what to fix if the server and client don't work. Is the server wrong? Is the client wrong? How can you tell if you don't have a specification to compare them both to? If you're going to exchange messages, you need a message protocol. TCP is not a message protocol. So you'll have to build one on top of TCP.
    – David Schwartz
    Nov 10 at 20:26











  • my server need to service a lot of clients
    – owczar95
    Nov 10 at 20:27










  • the main problem of this programm is execlp, without it everything working well. I mean instead of line in server execlp("./kropka","./kropka",buffer,NULL) use " cout<<buffer<<endl; "
    – owczar95
    Nov 10 at 20:32






  • 1




    @owczar95 Unfortunately, your code is working largely by luck. This makes it hard to find and identify the various problems because they aren't causing your code to fail. At a minimum, you absolutely must write functions to send and receive messages because your application requires you to be able to send and receive messages and nowhere do you have code that has any ability to do that. TCP is not a message protocol. You can build a message protocol on top of TCP, but you have to actually write code to do that.
    – David Schwartz
    Nov 10 at 20:35













up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1





I wanna make 3 programms- client, server and dotmaker.
In this case when i started in 2 terminal in first ./server, in second ./client and in client i wrote for example "abcd" i got just once "a.b.c.d", after this my server is not respoding.
I want enter a many messages and get a dotted message every time.



i got this:



./server
Czekam....
Connection accepted from 0.208.246.183:62647
g.o.o.d.b.y.e.

./client
Connected to Server.
Client: goodbye
Server: goodbye
Client: goodbye2


Here is the code



dot.cc



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 4444

#include <iostream>
using namespace std;

int main(int argc, char *argv)


string c="";
c=argv[1];

for(int i=0;i<strlen(argv[1]);i++)

cout<<c[i]<<".";

cout<<endl;

return 0;



client



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 4444

int main()

int clientSocket, i;
struct sockaddr_in serverAddr;
char buffer[1024];

clientSocket = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddr, '', sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

i = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
printf("Connected to Server.n");

while(1)
printf("Client: t");
scanf("%s", &buffer[0]);
send(clientSocket, buffer, strlen(buffer),0);
if(sizeof(buffer)==0)
printf("Disconnected from server.n");
close(clientSocket);
exit(1);

recv(clientSocket, buffer,1024,0);
printf("Server: t%sn", buffer);



return 0;



server.cc



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#define PORT 4444
#include <iostream>
using namespace std;
int main()

socklen_t addr_size;
char buffer[1024];
pid_t childpid;

int newSocket,status;
struct sockaddr_in newAddr;

int sock,i,j;
struct sockaddr_in serverAddr;

sock = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

i= bind(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr));

listen(sock, 10);
printf("Czekam....n");

while(1)
newSocket = accept(sock, (struct sockaddr*)&newAddr, &addr_size);
if(newSocket < 0)
exit(1);

printf("Connection accepted from %s:%dn", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
if((childpid = fork()) == 0)
close(sock);
while(1)

if(recv(newSocket, buffer, 1024,0)==0)
printf("Disconnected from %s:%dn", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
break;
else
sleep(1);
send(newSocket,buffer,strlen(buffer),0);
execlp("./kropka","./kropka",buffer,NULL);
exit(1);



else
wait(NULL);



close(newSocket);

return 0;










share|improve this question













I wanna make 3 programms- client, server and dotmaker.
In this case when i started in 2 terminal in first ./server, in second ./client and in client i wrote for example "abcd" i got just once "a.b.c.d", after this my server is not respoding.
I want enter a many messages and get a dotted message every time.



i got this:



./server
Czekam....
Connection accepted from 0.208.246.183:62647
g.o.o.d.b.y.e.

./client
Connected to Server.
Client: goodbye
Server: goodbye
Client: goodbye2


Here is the code



dot.cc



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 4444

#include <iostream>
using namespace std;

int main(int argc, char *argv)


string c="";
c=argv[1];

for(int i=0;i<strlen(argv[1]);i++)

cout<<c[i]<<".";

cout<<endl;

return 0;



client



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 4444

int main()

int clientSocket, i;
struct sockaddr_in serverAddr;
char buffer[1024];

clientSocket = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddr, '', sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

i = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
printf("Connected to Server.n");

while(1)
printf("Client: t");
scanf("%s", &buffer[0]);
send(clientSocket, buffer, strlen(buffer),0);
if(sizeof(buffer)==0)
printf("Disconnected from server.n");
close(clientSocket);
exit(1);

recv(clientSocket, buffer,1024,0);
printf("Server: t%sn", buffer);



return 0;



server.cc



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#define PORT 4444
#include <iostream>
using namespace std;
int main()

socklen_t addr_size;
char buffer[1024];
pid_t childpid;

int newSocket,status;
struct sockaddr_in newAddr;

int sock,i,j;
struct sockaddr_in serverAddr;

sock = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

i= bind(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr));

listen(sock, 10);
printf("Czekam....n");

while(1)
newSocket = accept(sock, (struct sockaddr*)&newAddr, &addr_size);
if(newSocket < 0)
exit(1);

printf("Connection accepted from %s:%dn", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
if((childpid = fork()) == 0)
close(sock);
while(1)

if(recv(newSocket, buffer, 1024,0)==0)
printf("Disconnected from %s:%dn", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
break;
else
sleep(1);
send(newSocket,buffer,strlen(buffer),0);
execlp("./kropka","./kropka",buffer,NULL);
exit(1);



else
wait(NULL);



close(newSocket);

return 0;







c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 20:08









owczar95

1




1











  • why do you need to fork?
    – OznOg
    Nov 10 at 20:15






  • 2




    Before you start writing any code, write down a specification for the protocol your server and client will use to exchange data. Otherwise, it will be impossible to determine what to fix if the server and client don't work. Is the server wrong? Is the client wrong? How can you tell if you don't have a specification to compare them both to? If you're going to exchange messages, you need a message protocol. TCP is not a message protocol. So you'll have to build one on top of TCP.
    – David Schwartz
    Nov 10 at 20:26











  • my server need to service a lot of clients
    – owczar95
    Nov 10 at 20:27










  • the main problem of this programm is execlp, without it everything working well. I mean instead of line in server execlp("./kropka","./kropka",buffer,NULL) use " cout<<buffer<<endl; "
    – owczar95
    Nov 10 at 20:32






  • 1




    @owczar95 Unfortunately, your code is working largely by luck. This makes it hard to find and identify the various problems because they aren't causing your code to fail. At a minimum, you absolutely must write functions to send and receive messages because your application requires you to be able to send and receive messages and nowhere do you have code that has any ability to do that. TCP is not a message protocol. You can build a message protocol on top of TCP, but you have to actually write code to do that.
    – David Schwartz
    Nov 10 at 20:35

















  • why do you need to fork?
    – OznOg
    Nov 10 at 20:15






  • 2




    Before you start writing any code, write down a specification for the protocol your server and client will use to exchange data. Otherwise, it will be impossible to determine what to fix if the server and client don't work. Is the server wrong? Is the client wrong? How can you tell if you don't have a specification to compare them both to? If you're going to exchange messages, you need a message protocol. TCP is not a message protocol. So you'll have to build one on top of TCP.
    – David Schwartz
    Nov 10 at 20:26











  • my server need to service a lot of clients
    – owczar95
    Nov 10 at 20:27










  • the main problem of this programm is execlp, without it everything working well. I mean instead of line in server execlp("./kropka","./kropka",buffer,NULL) use " cout<<buffer<<endl; "
    – owczar95
    Nov 10 at 20:32






  • 1




    @owczar95 Unfortunately, your code is working largely by luck. This makes it hard to find and identify the various problems because they aren't causing your code to fail. At a minimum, you absolutely must write functions to send and receive messages because your application requires you to be able to send and receive messages and nowhere do you have code that has any ability to do that. TCP is not a message protocol. You can build a message protocol on top of TCP, but you have to actually write code to do that.
    – David Schwartz
    Nov 10 at 20:35
















why do you need to fork?
– OznOg
Nov 10 at 20:15




why do you need to fork?
– OznOg
Nov 10 at 20:15




2




2




Before you start writing any code, write down a specification for the protocol your server and client will use to exchange data. Otherwise, it will be impossible to determine what to fix if the server and client don't work. Is the server wrong? Is the client wrong? How can you tell if you don't have a specification to compare them both to? If you're going to exchange messages, you need a message protocol. TCP is not a message protocol. So you'll have to build one on top of TCP.
– David Schwartz
Nov 10 at 20:26





Before you start writing any code, write down a specification for the protocol your server and client will use to exchange data. Otherwise, it will be impossible to determine what to fix if the server and client don't work. Is the server wrong? Is the client wrong? How can you tell if you don't have a specification to compare them both to? If you're going to exchange messages, you need a message protocol. TCP is not a message protocol. So you'll have to build one on top of TCP.
– David Schwartz
Nov 10 at 20:26













my server need to service a lot of clients
– owczar95
Nov 10 at 20:27




my server need to service a lot of clients
– owczar95
Nov 10 at 20:27












the main problem of this programm is execlp, without it everything working well. I mean instead of line in server execlp("./kropka","./kropka",buffer,NULL) use " cout<<buffer<<endl; "
– owczar95
Nov 10 at 20:32




the main problem of this programm is execlp, without it everything working well. I mean instead of line in server execlp("./kropka","./kropka",buffer,NULL) use " cout<<buffer<<endl; "
– owczar95
Nov 10 at 20:32




1




1




@owczar95 Unfortunately, your code is working largely by luck. This makes it hard to find and identify the various problems because they aren't causing your code to fail. At a minimum, you absolutely must write functions to send and receive messages because your application requires you to be able to send and receive messages and nowhere do you have code that has any ability to do that. TCP is not a message protocol. You can build a message protocol on top of TCP, but you have to actually write code to do that.
– David Schwartz
Nov 10 at 20:35





@owczar95 Unfortunately, your code is working largely by luck. This makes it hard to find and identify the various problems because they aren't causing your code to fail. At a minimum, you absolutely must write functions to send and receive messages because your application requires you to be able to send and receive messages and nowhere do you have code that has any ability to do that. TCP is not a message protocol. You can build a message protocol on top of TCP, but you have to actually write code to do that.
– David Schwartz
Nov 10 at 20:35


















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',
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
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242971%2ftcp-client-server-bsd-socket-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242971%2ftcp-client-server-bsd-socket-c%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto