#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define double 3
void *fun_send(void *arg)
{
char buf[20];
int newfd = *(int *)arg;
while(1){
scanf("%s",buf);
send(newfd,buf,strlen(buf),0);
}
pthread_exit(NULL);
}
int main(int argc,char *argv[])
{
#ifdef send_msg
if(argc != 3){
printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(1);
}
int soc_fd = socket(AF_INET,SOCK_STREAM,0);
if(soc_fd < 0){
perror("socket error");
exit(1);
}
int on =1;
setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
int ret_bind = bind(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(ret_bind<0){
perror("bind error");
exit(1);
}
int ret_listen = listen(soc_fd,5);
if(ret_listen<0){
perror("bind error");
exit(1);
}
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
int ret_accept = accept(soc_fd,(struct sockaddr *)&client_addr,&addr_len);
if(ret_accept<0){
perror("accept error");
exit(1);
}
char buf[20];
bzero(buf,sizeof(buf));
recv(ret_accept,buf,sizeof(buf),0);
printf("%s\n",buf);
close(ret_accept);
close(soc_fd);
#endif
#ifdef single
if(argc != 3){
printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(1);
}
int soc_fd = socket(AF_INET,SOCK_STREAM,0);
if(soc_fd < 0){
perror("socket error");
exit(1);
}
int on =1;
setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
int ret_bind = bind(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(ret_bind<0){
perror("bind error");
exit(1);
}
int ret_listen = listen(soc_fd,5);
if(ret_listen<0){
perror("bind error");
exit(1);
}
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
int ret_accept = accept(soc_fd,(struct sockaddr *)&client_addr,&addr_len);
if(ret_accept<0){
perror("accept error");
exit(1);
}
char buf[20];
int ret_recv;
while(1){
bzero(buf,sizeof(buf));
ret_recv = recv(ret_accept,buf,sizeof(buf),0);
if(ret_recv==0){
close(ret_accept);
close(soc_fd);
break;
}
printf("%s\n",buf);
}
#endif
#ifdef double
if(argc != 3){
printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(1);
}
int soc_fd = socket(AF_INET,SOCK_STREAM,0);
if(soc_fd < 0){
perror("socket error");
exit(1);
}
int on =1;
setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
int ret_bind = bind(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(ret_bind<0){
perror("bind error");
exit(1);
}
int ret_listen = listen(soc_fd,5);
if(ret_listen<0){
perror("bind error");
exit(1);
}
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
int ret_accept = accept(soc_fd,(struct sockaddr *)&client_addr,&addr_len);
if(ret_accept<0){
perror("accept error");
exit(1);
}
int ret_pthread_create;
pthread_t pthread_id;
ret_pthread_create = pthread_create(&pthread_id,NULL,fun_send,(void *)&ret_accept);
if(ret_pthread_create!=0){
printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(1);
}
char buf[20];
int ret_recv;
while(1){
bzero(buf,sizeof(buf));
ret_recv = recv(ret_accept,buf,sizeof(buf),0);
if(ret_recv==0){
close(ret_accept);
close(soc_fd);
break;
}
printf("%s\n",buf);
}
#endif
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define double 3
void *fun_recv(void *arg)
{
char buf[20];
int soc_fd = *(int *)arg;
int ret_recv;
while(1){
bzero(buf,sizeof(buf));
ret_recv = recv(soc_fd,buf,sizeof(buf),0);
if(ret_recv==0){
close(soc_fd);
exit(0);
}
printf("%s\n",buf);
}
pthread_exit(NULL);
}
int main(int argc,char *argv[])
{
#ifdef send_msg
if(argc != 3){
printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(1);
}
int soc_fd = socket(AF_INET,SOCK_STREAM,0);
if(soc_fd < 0){
perror("socket error");
exit(1);
}
int on =1;
setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
struct sockaddr_in client_addr;
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(6666);
client_addr.sin_addr.s_addr = inet_addr("192.168.90.74");
int ret_bind = bind(soc_fd,(struct sockaddr *)&client_addr,sizeof(client_addr));
if(ret_bind<0){
perror("bind error");
exit(1);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
int ret_connect = connect(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(ret_connect<0){
perror("connect error");
exit(1);
}
char buf[20]="hello world";
send(soc_fd,buf,strlen(buf),0);
close(soc_fd);
#endif
#ifdef single
if(argc != 3){
printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(1);
}
int soc_fd = socket(AF_INET,SOCK_STREAM,0);
if(soc_fd < 0){
perror("socket error");
exit(1);
}
int on =1;
setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
struct sockaddr_in client_addr;
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(6666);
client_addr.sin_addr.s_addr = inet_addr("192.168.90.74");
int ret_bind = bind(soc_fd,(struct sockaddr *)&client_addr,sizeof(client_addr));
if(ret_bind<0){
perror("bind error");
exit(1);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
int ret_connect = connect(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(ret_connect<0){
perror("connect error");
exit(1);
}
char buf[20];
while(1){
bzero(buf,sizeof(buf));
gets(buf);
send(soc_fd,buf,strlen(buf),0);
}
close(soc_fd);
#endif
#ifdef double
if(argc != 3){
printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(1);
}
int soc_fd = socket(AF_INET,SOCK_STREAM,0);
if(soc_fd < 0){
perror("socket error");
exit(1);
}
int on =1;
setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
struct sockaddr_in client_addr;
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(6666);
client_addr.sin_addr.s_addr = inet_addr("192.168.90.74");
int ret_bind = bind(soc_fd,(struct sockaddr *)&client_addr,sizeof(client_addr));
if(ret_bind<0){
perror("bind error");
exit(1);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
int ret_connect = connect(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(ret_connect<0){
perror("connect error");
exit(1);
}
int ret_pthread_create;
pthread_t pthread_id;
ret_pthread_create = pthread_create(&pthread_id,NULL,fun_recv,(void *)&soc_fd);
if(ret_pthread_create!=0){
printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(1);
}
char buf[20];
while(1){
bzero(buf,sizeof(buf));
gets(buf);
send(soc_fd,buf,strlen(buf),0);
}
close(soc_fd);
pthread_join(pthread_id,NULL);
#endif
return 0;
}