Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337152
  • 博文数量: 64
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 589
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-08 15:50
文章分类

全部博文(64)

文章存档

2015年(52)

2014年(3)

2013年(9)

我的朋友

分类: WINDOWS

2015-07-15 18:01:58

windows系统的客户端:

#include <stdio.h>
#include <Windows.h>
 
#pragma comment(lib,"ws2_32.lib")
#define  PORT 4000
#define  IP_ADDRESS "192.168.1.128"
 
int main(int argc, char* argv[])
{
    WSADATA  Ws;
    SOCKET ClientSocket;
    struct sockaddr_in ClientAddr;
    int Ret = 0;
    char SendBuffer[MAX_PATH];


    /* Init Windows Socket */
    if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
    {
        printf("Init Windows Socket Failed::%d\n", GetLastError());
        return -1;
    }
 
    /* Create Socket */
    ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if ( ClientSocket == INVALID_SOCKET )
    {
        printf("Create Socket Failed::%d\n", GetLastError());
        return -1;
    }


    ClientAddr.sin_family = AF_INET;
    ClientAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
    ClientAddr.sin_port = htons(PORT);
    memset(ClientAddr.sin_zero, 0x00, 8);


/* connect socket */
    Ret = connect(ClientSocket,(struct sockaddr*)&ClientAddr, sizeof(ClientAddr));
    if ( Ret == SOCKET_ERROR )


{        printf("Connect Error::%d\n", GetLastError());
        return -1;
    }
    else
    {
        printf("Connect succedded!\n");
    }


    while (1)
    {
        scanf("%s", SendBuffer);
/* send data to server */
        Ret = send(ClientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
        if ( Ret == SOCKET_ERROR )
        {
            printf("Send Info Error::%d\n", GetLastError());
            break;
        }

if('q' == SendBuffer[0])
{
break;
}
    }
    
/* close socket */
    closesocket(ClientSocket);
    WSACleanup();
 
    return 0;
 }


Linux系统的服务端:

#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define HELLO_WORLD_SERVER_PORT    4000
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 1024


int main(int argc, char **argv)
{
    struct sockaddr_in server_addr;
int server_socket;
int opt = 1;


    bzero(&server_addr,sizeof(server_addr));


    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htons(INADDR_ANY);
    server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);


/* create a socket */
    server_socket = socket(PF_INET,SOCK_STREAM,0);
    if( server_socket < 0)
    {
        printf("Create Socket Failed!");
        exit(1);
    }


    /* bind socket to a specified address*/
    setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
    if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)))
    {
        printf("Server Bind Port : %d Failed!", HELLO_WORLD_SERVER_PORT);
        exit(1);
    }


    /* listen a socket */
    if(listen(server_socket, LENGTH_OF_LISTEN_QUEUE))
    {
        printf("Server Listen Failed!");
        exit(1);
    }


/* run server */
    while (1)
    {
        struct sockaddr_in client_addr;
int client_socket;
        socklen_t length;
char buffer[BUFFER_SIZE];


/* accept socket from client */
length = sizeof(client_addr);
        client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);
        if( client_socket < 0)
        {
            printf("Server Accept Failed!\n");
            break;
        }


/* receive data from client */
while(1)
{
bzero(buffer, BUFFER_SIZE);
length = recv(client_socket, buffer, BUFFER_SIZE, 0);
if (length < 0)
{
printf("Server Recieve Data Failed!\n");
break;
}


if('q' == buffer[0])
{
printf("Quit from client!\n");
break;
}


printf("%s\n", buffer);
}


        close(client_socket);
    }


    close(server_socket);
    return 0;
}

可能存在的问题:

1、虚拟机选择NAT模式连接网络
2、关闭Linux系统的防火墙,否则Windows不能够连接Linux
(1) 重启后永久性生效:   开启:chkconfig iptables on   关闭:chkconfig iptables off   
(2) 即时生效,重启后失效:   开启:service iptables start   关闭:service iptables stop
阅读(2551) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~