62 lines
1.2 KiB
C
62 lines
1.2 KiB
C
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <netinet/tcp.h>
|
|
#include <netdb.h>
|
|
|
|
|
|
int main(int argc, const char * argv[]) {
|
|
|
|
int client_sockfd;
|
|
int len;
|
|
struct sockaddr_in remota_addr;
|
|
char buf[BUFSIZ];
|
|
|
|
memset(&remota_addr,0,sizeof(remota_addr));
|
|
remota_addr.sin_family = AF_INET;
|
|
remota_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
remota_addr.sin_port = htons(8000);
|
|
|
|
|
|
|
|
|
|
client_sockfd=socket(PF_INET, SOCK_STREAM, 0);
|
|
connect(client_sockfd, (struct sockaddr *)&remota_addr, sizeof(struct sockaddr));
|
|
|
|
printf("connect to server\n");
|
|
|
|
len = recv(client_sockfd, buf, BUFSIZ, 0);
|
|
buf[len]='\0';
|
|
printf("%s",buf);
|
|
|
|
|
|
|
|
while (1) {
|
|
memset(buf,0,sizeof(buf));
|
|
|
|
printf("Enter string to send");
|
|
scanf("%s",buf);
|
|
|
|
len=send(client_sockfd,buf,strlen(buf),0);
|
|
memset(buf,0,sizeof(buf));
|
|
|
|
len=recv(client_sockfd,buf,BUFSIZ,0);
|
|
buf[len]='\0';
|
|
printf("received:%s\n",buf);
|
|
if(buf[0]=='q'){
|
|
break;
|
|
}
|
|
|
|
}
|
|
close(client_sockfd);
|
|
return 0;
|
|
}
|
|
|