Hewlett-Packard Co. said it agreed to buy networking-gear maker 3Com Corporation for $2.7 billion in cash.