Skip to content

Instantly share code, notes, and snippets.

@CreatiCoding
Last active August 10, 2018 06:55
Show Gist options
  • Save CreatiCoding/4ffef53fd67578792a7ec4b3f739e38f to your computer and use it in GitHub Desktop.
Save CreatiCoding/4ffef53fd67578792a7ec4b3f739e38f to your computer and use it in GitHub Desktop.
c++ socket tcp one process one thread increase one server and client
#include <cstdio>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>
using namespace std;
void error(const char *message){
perror(message);
exit(1);
}
int main(int argc,char **argv){
if(argc<3) error("argument is ip and port.");
int server_socket;
int port;
struct hostent *server_url;
struct sockaddr_in server_addr;
string input;
int check;
port=atoi(argv[2]);
server_socket=socket(AF_INET,SOCK_STREAM,0);
if(server_socket<0) error("error occurs in opening socket.");
server_url=gethostbyname(argv[1]);
if(server_url<0) error("error occurs, because no such host.");
server_addr.sin_family=AF_INET;
bcopy((char *)server_url->h_addr,(char *)&server_addr.sin_addr.s_addr,server_url->h_length);
server_addr.sin_port=htons(port);
check=connect(server_socket,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(check<0) error("error occurs in connecting");
while(true){
cout<<"input the number: ";
cin>>input;
if(string("exit").compare(input) == 0 || string("shutdown").compare(input) == 0){
cout<<input<<" the server."<<endl;
write(server_socket, input.c_str(), input.length() );
close(server_socket);
exit(0);
}else{
try {
stoi(input);
} catch(...) {
cout<<"Try again."<<endl;
continue;
}
check = write(server_socket, input.c_str(), input.length() );
if(check<0) error("disconnect, server shutdowned.");
char buffer[256] = { 0 };
check = read(server_socket,buffer,256);
if(check<=0) error("disconnect, server shutdowned.");
cout<<"result: "<<buffer<<endl;
}
}
}
#include <cstdio>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <iostream>
using namespace std;
void error(const char *message){
perror(message);
exit(1);
}
int main(int argc,char **argv){
if(argc<2) error("argument is port.");
int server_socket, client_socket;
int port;
socklen_t client_socket_length;
struct sockaddr_in server_addr,client_addr;
int queue_length;
int check;
port=atoi(argv[1]);
queue_length=128;
server_socket=socket(AF_INET,SOCK_STREAM,0);
if(server_socket<0) error("error occurs in opening server_socket");
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=INADDR_ANY;
server_addr.sin_port=htons(port);
check = bind(server_socket,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(check<0) error("error occurs in binding");
check = listen(server_socket,queue_length);
if(check<0) error("error occurs in listening");
bool isOver = false;
while(!isOver){
client_socket_length=sizeof(client_addr);
client_socket=accept(server_socket,(struct sockaddr *)&client_addr,&client_socket_length);
if(client_socket<0) error("error occurs in accepting");
else cout<<"client enters."<<endl;
while(true){
char buffer[256] = { 0 };
check = read(client_socket,buffer,256);
if(check <= 0) {
cout << "disconnect, client exited." << endl;
close(client_socket);
break;
}else if(string("exit").compare(buffer) == 0){
cout << "client exited." << endl;
close(client_socket);
break;
}else if(string("shutdown").compare(buffer) == 0){
cout << "server will shutdown." << endl;
write(client_socket,"shutdown",9);
close(client_socket);
isOver = true;
break;
}
int output = 0;
string message;
try {
output = stoi(buffer) + 1;
cout<<"client: "<<buffer<<endl;
cout<<"result: "<<output<<endl;
message=to_string(output);
} catch(...) {
message="Try again, input number.";
}
check=write(client_socket,message.c_str(),message.length());
if(check<0){
cout << "disconnect, client exited." << endl;
close(client_socket);
break;
}
}
}
close(server_socket);
close(client_socket);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment