#include //for sockaddr_in
#include //for socket
#include //for socket
#include //for printf
#include //for ext
#include //for bzero
int main(int argc,char *argv[])
{
int sock_fd;
char rbuf[200],sbuf[200];
struct sockaddr_in serv_addr;
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(6621);
serv_addr.sin_addr.s_addr=htons(INADDR_ANY);
bzero(&(serv_addr.sin_zero),8);
sock_fd=socket(AF_INET,SOCK_STREAM,0);
connect(sock_fd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr));
while(1)
{
int recvhow=recv(sock_fd,rbuf,200,0);
rbuf[recvhow]='\0';
/*
if(rbuf[0]=='#')
break;
else
{
printf("server says:\t%s",rbuf);
bzero(rbuf,200);
*/
gets(sbuf);
printf("%s",sbuf);
send(sock_fd,"hello world!",strlen(sbuf),0);
// }
}
close(sock_fd);
return 0;
}
#include //for sockaddr_in
#include //for socket
#include //for socket
#include //for printf
#include //for ext
#include //for bzero
int main(int argc,char *argv)
{
char rbuf[200];
char sbuf[200];
int server_socket,new_socket;
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(6621);
bzero(&(server_addr.sin_zero),8);
server_socket=socket(AF_INET,SOCK_STREAM,0);
if(server_socket<0)
{
printf("Failed to create Socket!\n");
exit(1);
}
if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))== -1 )
{
printf("Faild to bind!\n");
exit(1);
}
if( listen(server_socket,20)==-1)
{
printf("Failed to listen!\n");
exit(1);
}
while(1)
{
struct sockaddr_in client_addr;
int sin_size=sizeof(client_addr);
new_socket=accept(server_socket,(struct sockaddr*)&client_addr,&sin_size);
if(new_socket==-1)
{
printf("Failed to accept!\n");
break;
}
int num=recv(new_socket,rbuf,200,0);
rbuf[num]='\0';
/*
if(rbuf[0]=='#')
break;
else
{*/
printf("client says:\t%s",rbuf);
/*bzero(rbuf,200);
gets(sbuf);
printf("%s",sbuf);
send(new_socket,sbuf,strlen(sbuf),0);
}*/
}
close(new_socket);
close(server_socket);
return 0;
}
阅读(804) | 评论(0) | 转发(0) |