Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<arpa/inet.h>
- #include<ncurses.h>
- #include<time.h>
- #include<signal.h>
- void error(const char *msg){
- perror(msg);
- exit(1);
- }
- void base_start(char *ip,int port,int *my_socket,struct sockaddr_in *socket_addr,struct sockaddr_in *server_addr){
- *my_socket=socket(AF_INET,SOCK_STREAM,0);
- int opt=1;
- setsockopt(*my_socket,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
- if(*my_socket<0){
- error("mysocket");
- }
- /*создали сокет клиента*/
- socket_addr->sin_family=AF_INET;
- socket_addr->sin_port=htons(1235);
- socket_addr->sin_addr.s_addr=htons(INADDR_ANY);
- /*заполнили структуру адреса*/
- if(bind(*my_socket,(struct sockaddr*)socket_addr,sizeof(*socket_addr))<0){
- error("bind");
- }
- /*привязали адресс к сокету*/
- server_addr->sin_family=AF_INET;
- server_addr->sin_port=htons(12345);
- server_addr->sin_addr.s_addr=inet_addr(ip);
- if(server_addr->sin_addr.s_addr<0)
- error("addr");
- }
- void SendServer_char(char *buf,int my_socket,struct sockaddr_in *server_addr){
- if(sendto(my_socket,buf,1,0,(struct sockaddr *)server_addr,sizeof(*server_addr))<0){
- error("send");
- }
- }
- void handler(int s){
- printw("+");
- }
- typedef struct{
- char name[32];
- char password[32];
- } User;
- int main(int argc,char *argv[]){
- signal(SIGUSR1,handler);
- initscr();
- cbreak();
- keypad(stdscr,1);
- nodelay(stdscr,1);
- User grisha={
- "grisha",
- "pasw",
- };
- struct sockaddr_in socket_addr,server_addr;
- int my_socket;
- base_start(argv[1],12345,&my_socket,&socket_addr,&server_addr);
- socklen_t size=(socklen_t)sizeof(server_addr);
- if(connect(my_socket,(struct sockaddr *)&server_addr,size)<0)
- error("connect");
- int res=write(my_socket,&grisha,sizeof(grisha));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement