S
S
Stasyn19902015-04-09 21:09:54
Python
Stasyn1990, 2015-04-09 21:09:54

How to write an application for a local network (under Linux)?

There are two computers (on Linux) connected by a wire. I set up the connection, checked the ping, it works. I wanted to try using sockets to send a message from one computer to another. wrote two mini-applications: the first creates a socket with parameters af_inet, sock_stream; then gives it a name (using the bind system call); then the socket listens for a connection, and if the connection is established, data is read from the socket.
the second application creates a socket with parameters af_inet, sock_stream; after the application tries to connect to the given address, if connect is successful, then data is written to the socket (of the first application).
At first I ran these mini applications on one computer, everything worked. then transferred one application to the second computer, in this case the applications do not work correctly. The 2nd application (which sends a message to the socket to another application) simply cannot connect.
I tried it and broke my head like that. True programmers, tell me where the problem may be.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nick Sdk, 2018-03-20
@IvankoPo

https://regex101.com/r/lm7gd3/1
<pos[^<>]*?>\s*(.*?)<\/pos>

A
Armenian Radio, 2015-04-09
@gbg

Read Sneijder - Efficient TCP/IP Programming
And show the source. It will be clearer than a brick of text.

S
Stasyn1990, 2015-04-09
@Stasyn1990

#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include <string.h>
#include <sys/shm.h>
#include <cstring>
#include <memory.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <strings.h>
#include <netdb.h>
#include <arpa/inet.h>

extern int errno;
#define SOCKETNAME "me"

using namespace std;

int main (int argc, char **argv){

int acpt;
int ls;


int maxlog = 5;


struct sockaddr sk;
struct sockaddr_in servadr;


int fds, ibind, getadr;

int rd, rw;
int op;

int stsk;

ssize_t size = 6;
ssize_t s;
char buff[128];
int prot = 0;

fds = socket(AF_INET, SOCK_STREAM, prot);
if(fds < 0){perror("error: ");}
else{
std::cout << "сокет создан его ФД: "<< fds <<std::endl;
bzero((char *) &servadr, sizeof(servadr));

int sizebuf = 65000;
uint16_t port = 20000;
uint32_t ip = 19216801;


servadr.sin_family = AF_INET;
servadr.sin_port = htons(20000);
servadr.sin_addr.s_addr = inet_addr("192.168.0.1");
ibind = bind(fds, (struct sockaddr *) &servadr, sizeof(servadr));
if(ibind < 0) { perror("error systemcall bind: "); }
else{ 
std::cout <<"назначено имя сокету" <<std::endl; 
ls = listen(fds,maxlog);
if(ls < 0){ perror("error systemcall listen: "); }
else{ std::cout << "сокет слушает" <<std::endl; }
acpt = accept(fds, NULL, 0);
int i;
for(i = 0; i < 40; i++){
if(acpt < 0 || acpt == 0){ perror("error sk accept: "); }
else{  
s = read(acpt, buff, sizeof(buff));
if(s < 0 || s == 0){ std::cout << "error" <<std::endl; }  
else {std::cout << buff <<std::endl;}
}
  continue;
  }
 
if(i == 40){
close(acpt);
close(fds);
}
 }
   }

return 0;
}
// это первое приложение

#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include<sys/types.h> 
#include<sys/ipc.h> 
#include<sys/msg.h> 
#include <string.h> 
#include <sys/shm.h>
#include <cstring>
#include <memory.h> 
#include <sys/socket.h> 
#include <sys/un.h>
#include <netinet/in.h>
#include <strings.h>
#include <netdb.h>
#include <arpa/inet.h> 
    

#define REQUEST "hello"
#define SOCKETNAME "me"
    
extern int errno;

using namespace std;

int main (int argc, char **argv){

int cn;

struct sockaddr_in srd;


int fds;
ssize_t swr = 6;
ssize_t s;
char c[6] = "hello";

srd.sin_family = AF_INET;
srd.sin_port = htons(20000);
srd.sin_addr.s_addr = inet_addr("192.168.0.1");
fds = socket(AF_INET, SOCK_STREAM, 0);
if(fds < 0){ perror("error sk socket: "); }
else{ std::cout << "сокет создан его ФД: " <<std::endl;
cn = connect(fds, (struct sockaddr *) &srd, sizeof(srd));
if(cn < 0){ perror("error sk connect: "); }
else{
std::cout << "соединение установлено" <<std::endl;
for(int i = 0; i < 20; i++){
s = write(fds, &c, swr);
if(s > 0){ std::cout << s <<std::endl; }
sleep(10);
continue;}
close(fds);
}
  }
return 0;


}
// это второе приложение

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question