Linux中用C++寫基本TCP/IP client
這是在Linux中用C++以及Linux的system call
寫的一個基本的TCP/IP client
架設client的基本routine就是這樣而已
需要的時候就直接複製使用吧
下面是我寫的程式碼
程式碼解釋我哪天有空再來補(感覺是不會補了哈哈
結果的截圖我就不放了
反正就是client
比較值得注意的是,這個是用select來檢測tcp跟std::in(client端鍵盤輸入)哪個有訊息要讀
大致上就是這樣
好了,今天的筆記到此結束
希望有幫助未來遺忘這些的自己,以及需要的人
寫的一個基本的TCP/IP client
架設client的基本routine就是這樣而已
需要的時候就直接複製使用吧
下面是我寫的程式碼
程式碼解釋我哪天有空再來補(感覺是不會補了哈哈
//tcpclient.cpp
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <signal.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/socket.h> /*for BSD*/
#define BUFF_SIZE 100000
int connectTCP(std::string ip, std::string port){
int sockfd;
struct sockaddr_in client_sin;
struct hostent *host_ip;
int SERV_TCP_PORT;
std::stringstream ss;
/*Open a TCP socket(an Internet stream socket).*/
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
std::cerr << "server: can't open stream socket" << std::endl;
/*fill in the structure "client_sin" with the adderss of the server*/
if((host_ip=gethostbyname(ip.c_str())) == NULL)
std::cerr << "illegal input arguments" << std::endl;
ss.clear();
ss.str(port);
ss >> SERV_TCP_PORT;
bzero((char *)&client_sin, sizeof(client_sin));
client_sin.sin_family = AF_INET;
client_sin.sin_addr = *((struct in_addr *)host_ip->h_addr);
client_sin.sin_port = htons(SERV_TCP_PORT);
/*connect to server*/
if(connect(sockfd,(struct sockaddr *)&client_sin,sizeof(client_sin)) == -1){
std::cerr << "connect to server error" << std::endl;
exit(0);
}
return sockfd;
}
int main(int argc, char *argv[]){
int sockfd = 0;
int num_rstdin = 0;
int num_rsocket = 0;
std::string str_port = "";
std::string str_ip = "";
std::stringstream ss;
char char_rstdin;
char char_rsocket;
std::string str_rstdin = "";
fd_set rfds;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100;
/*connect to server*/
ss.clear();
ss.str(argv[1]);
ss >> str_ip;
ss.clear();
ss.str(argv[2]);
ss >> str_port;
sockfd = connectTCP(str_ip,str_port);
/*non-blocking read from stdin and server, then process the data*/
while(1){
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
FD_SET(sockfd, &rfds);
select(sockfd+1, &rfds, NULL, NULL,&timeout); /*block here, almost non-block(timeout is short)*/
if(FD_ISSET(STDIN_FILENO, &rfds)){
num_rstdin = read(STDIN_FILENO, &char_rstdin, 1);
if(char_rstdin != '\n') /*split stdin input data with '\n'*/
str_rstdin += char_rstdin;
else{
write(sockfd, str_rstdin.c_str(), str_rstdin.size());
write(sockfd, "\n", 1); /*remote server splite data with '\n'*/
str_rstdin = "";
}
}
if(FD_ISSET(sockfd, &rfds)){
char chararr_rsocket[BUFF_SIZE] = {0};
num_rsocket = read(sockfd,chararr_rsocket,BUFF_SIZE);
if(num_rsocket==0)
return 0;
else if(num_rsocket<0)
std::cout << "read socket error" << std::endl;
std::cout << chararr_rsocket << std::flush;
}/*if(FD_ISSET ... */
}/*while loop*/
}/*int main()*/
結果的截圖我就不放了
反正就是client
比較值得注意的是,這個是用select來檢測tcp跟std::in(client端鍵盤輸入)哪個有訊息要讀
大致上就是這樣
希望有幫助未來遺忘這些的自己,以及需要的人

留言
張貼留言