This commit is contained in:
ada-dmitry
2023-10-25 10:48:50 +03:00
parent b74967a89e
commit e09d60ff11
2 changed files with 65 additions and 50 deletions
+31 -24
View File
@@ -10,48 +10,55 @@
//./client_app stud*** "port"
#define DATA "Hello, World!"
int main(int argc, char *argv[]){
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_in server;
struct hostent *hp;
char buf[1024];
if (argc < 3) {
printf("c_inet hostname port [data]\n");
_exit(1);
}
if((sock = socket( AF_INET, SOCK_STREAM, 0)) == -1) {
perror("opening stream socket");
exit(1);
if (argc < 3)
{
printf("c_inet hostname port [data]\n");
_exit(1);
}
hp=malloc(sizeof(struct hostent));
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("opening stream socket");
exit(1);
}
hp = malloc(sizeof(struct hostent));
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp == (struct hostent *) 0) {
fprintf(stderr, "%s: unknown host\n", argv[1]);
exit(2);
if (hp == (struct hostent *)0)
{
fprintf(stderr, "%s: unknown host\n", argv[1]);
exit(2);
}
memcpy((char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length);
memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
server.sin_port = htons(atoi(argv[2]));
if (connect(sock, (struct sockaddr *) &server, sizeof server) == -1) {
if (connect(sock, (struct sockaddr *)&server, sizeof server) == -1)
{
perror("connecting stream socket");
exit(1);
}
getchar();
memset(buf,0,1024);
if (argc > 3){
printf("[%s]\n",argv[3]);
strcpy(buf,argv[3]);
}else{
strcpy(buf,DATA);
memset(buf, 0, 1024);
if (argc > 3)
{
printf("[%s]\n", argv[3]);
strcpy(buf, argv[3]);
}
else
{
strcpy(buf, DATA);
}
if (write(sock, buf, strlen(buf)) == -1)
perror("writing on stream socket");
perror("writing on stream socket");
close(sock);
exit(0);
}