头文件:
#ifndef _MYHEAD_H_
#define _MYHEAD_H_
#define _MYHEAD_H_
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#define MYPORT 6667 //1024以下的是保留的端口号,用大于1024的;
//#define MYADDR "192.168.1.102"
//#define MYADDR "192.168.1.102"
#endif
服务器程序:
#include "myhead.h"
void *read_msg(void *argc);
int main()
{
int ret = 0;
int socketfd = 0; //局部变量保存在栈空间,而栈空间是脏的==》里面还是保存的是上一次这个区域里面保存的值;
int clientfd = 0;
pid_t pid = 0;
pthread_t th = 0;
struct sockaddr_in sock_server = {0}; //变量类型保存在netinet/in.h里面的;
struct sockaddr_in sock_client = {0}; //保存连接的客户端那边的信息;
socklen_t len = sizeof(struct sockaddr);
//第一步:创建套接字;
socketfd = socket(AF_INET,SOCK_STREAM,0);
if(socketfd == -1)
{
perror("socket");
// printf("socket error!\n");
return -1;
{
int ret = 0;
int socketfd = 0; //局部变量保存在栈空间,而栈空间是脏的==》里面还是保存的是上一次这个区域里面保存的值;
int clientfd = 0;
pid_t pid = 0;
pthread_t th = 0;
struct sockaddr_in sock_server = {0}; //变量类型保存在netinet/in.h里面的;
struct sockaddr_in sock_client = {0}; //保存连接的客户端那边的信息;
socklen_t len = sizeof(struct sockaddr);
//第一步:创建套接字;
socketfd = socket(AF_INET,SOCK_STREAM,0);
if(socketfd == -1)
{
perror("socket");
// printf("socket error!\n");
return -1;