Results 1 to 3 of 3

Thread: ถาม : การเขียนภาษาซีในการตรวจสอบพอร์ต

  1. #1


    คือตอนนี้มี assignment วิชา network ครับผม อาจารย์ให้เขียนโปรแกรมแสกนพอร์ต ว่ามีพอร์ตใดของคอมพิวเตอร์ที่เปิดอยู่บ้าง
    ไม่ทราบว่าต้องไปศึกษาตรงไหนครับ มีฟังก์ชั่นอะไรในภาษาซีที่ติดต่อพอร์ตได้ครับ
    ตอนนี้รู้เรื่องการเขียนภาษาซี แต่ไม่รู้ว่าจะเอาภาษาซีมาเขียนโปรแกรมแสกนพอร์ตยังไงครับ เพราะไม่มีหนังสือภาษาซีเล่มไหนสอน

    ขอบคุณมากมายครับ
    [color=#33FF33]:: ความขี้เกียจ ทำให้คนเราโง่ ::[/color]

  2. #2
    Senior Member
    Join Date
    Jul 2004
    Location
    Thailand
    Posts
    211


    #include <stdio.h>
    #include <winsock.h>

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

    WSADATA wsaData;
    WSAStartup(MAKEWORD(1, 1), &wsaData);

    if(argc < 3) {
    printf("Usage: %s <ip/host> <startport> <endport> \n", argv[0]);
    exit(1);
    }

    struct hostent *he;
    struct sockaddr_in host;
    int sock;
    int start = atoi(argv[2]);
    int end = atoi(argv[3]);

    if((he=gethostbyname(argv[1])) == NULL) {
    printf("Could not resolve host or ip \n");
    exit(1);
    }
    printf("Found Host [ OK ]\n");
    printf("Initializing socket [ OK ]\n");
    printf("Connecting to Host [ OK ]\n");
    printf("Scanning %s\n\n", argv[1]);


    for(start; start < end; start++) {
    if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
    }


    host.sin_family = AF_INET;
    host.sin_port = htons(start);
    host.sin_addr = *((struct in_addr *)he->h_addr);

    if(connect(sock, (struct sockaddr *)&host, sizeof(struct sockaddr)) == -1) {
    closesocket(sock);
    }
    else {
    printf("Port %d open \n", start);
    closesocket(sock);
    }
    }
    return 0;
    }


    //By: logicport





    [/b]

  3. #3


    โห. . .พี่ครับ ขอบคุณมากมายเลยครับ ให้มาทั้ง sourcecode เลย
    ผมจะเอาไปศึกษาต่อครับ
    //รอดตายแล้วเรา
    [color=#33FF33]:: ความขี้เกียจ ทำให้คนเราโง่ ::[/color]

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •